【发布时间】:2016-08-11 22:16:24
【问题描述】:
我想在一个表中添加一列,我希望它是一个 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。
有什么方法可以实现吗?还是我的示例代码有问题?
【问题讨论】:
-
尝试在您的解决方案 #2 中的第一个
alter do end之后添加flush()。这行得通吗? -
有效!谢谢!
-
嗯...相同的代码引发
** (ArgumentError) argument error。有什么问题?要点 - gist.github.com/maslenkov/1ca80dbc913f92a9334a6545eb89880e -
好的。这是 Elixometer 的问题。我想我会在那里发布问题......
标签: elixir phoenix-framework ecto