How to run updating in migration for Ecto?

落爺英雄遲暮 提交于 2020-01-21 03:11:06

问题


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 data, so I decide to add the column, update all rows to some value and modify the column to NOT NULL.

I tried these two code:

  # solution 1
  def up do
    alter table(:channels) do
      add :type, :integer
      Exchat.Repo.update_all("channels", set: [type: 1])
      modify :type, :integer, null: false
    end
  end

  # solution 2
  def up do
    alter table(:channels) do
      add :type, :integer
    end
    Exchat.Repo.update_all("channels", set: [type: 1])
    alter table(:channels) do
      modify :type, :integer, null: false
    end
  end

Both of them didn't work and I got error like:

23::40::07.514 [info]  == Running Exchat.Repo.Migrations.AddTypeToChannels.up/0 forward

23::40::07.541 [debug] UPDATE "channels" AS c0 SET "type" = $1 [1] ERROR query=12.0ms
** (Postgrex.Error) ERROR (undefined_column): column "type" of relation "channels" does not exist
    (ecto) lib/ecto/adapters/sql.ex:383: Ecto.Adapters.SQL.execute_and_cache/7

I'm not sure if it's related to the Ecto version but my Ecto version is 2.0.0-rc.0.

Is there any way to achieve this? Or is there something wrong in my example code?


回答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


来源:https://stackoverflow.com/questions/36723407/how-to-run-updating-in-migration-for-ecto

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!