Elixir 如何在针对EXTO的迁移中运行更新?

Elixir 如何在针对EXTO的迁移中运行更新?,elixir,phoenix-framework,ecto,Elixir,Phoenix Framework,Ecto,我在我的一个项目中使用和 我想在一个表中添加一列,我希望它是一个非空列。但我已经有一些现有数据,所以我决定添加列,将所有行更新为某个值,并将列修改为NOTNULL 我尝试了以下两种代码: # solution 1 def up do alter table(:channels) do add :type, :integer Exchat.Repo.update_all("channels", set: [type: 1]) modify :ty

我在我的一个项目中使用和

我想在一个表中添加一列,我希望它是一个非空列。但我已经有一些现有数据,所以我决定添加列,将所有行更新为某个值,并将列修改为NOTNULL

我尝试了以下两种代码:

  # 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
两个都不起作用,我得到的错误如下:

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
我不确定它是否与外星版本有关,但我的外星版本是2.0.0-rc.0

有没有办法做到这一点?或者我的示例代码中有什么错误吗?

解决方案#2是正确的方法,但是您需要在第一个
alter
块之后添加对的调用,以确保立即在数据库上执行所有更改,而不是排队等待稍后执行,这是默认情况

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

尝试在解决方案2中的第一个
alter do end
之后添加
flush()
。行得通吗?行得通!非常感谢。陛下相同的代码引发
**(ArgumentError)参数错误
。有什么不对劲?要点-好的。这是elixometer的问题。我想我会把这个问题贴在那里。。。