How to run updating in migration for Ecto?

后端 未结 1 686
眼角桃花
眼角桃花 2021-01-02 07:23

I use Phoenix and Ecto in one of my project.

I want to add a column to one table, and I expect it to be a NOT NULL column. But I already have some existed d

相关标签:
1条回答
  • Solution #2 is the correct approach, but you need to add a call to Ecto.Migration.flush/0 after the first alter block to make sure all the changes are executed on the database instantly instead of being queued up to be executed later, which is the default.

    def up do
      alter table(:channels) do
        add :type, :integer
      end
      flush()
      Exchat.Repo.update_all("channels", set: [type: 1])
      alter table(:channels) do
        modify :type, :integer, null: false
      end
    end
    
    0 讨论(0)
提交回复
热议问题