I use Phoenix and Ecto在我的一个项目中。
我想向一个表添加一列,并且我希望它是一个NOT NULL柱子。但我已经有一些现有的数据,所以我决定添加列,将所有行更新为某个值并将列修改为NOT NULL.
我尝试了这两个代码:
# 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
我不确定它是否与 Ecto 版本有关,但我的 Ecto 版本是 2.0.0-rc.0。
有什么办法可以实现这一点吗?或者我的示例代码有问题吗?