cod*_*ted 3 elixir ecto phoenix-framework
我正在和 Phoenix 一起玩,并建立了 has_many 关联。我通常将其on_delete: :delete_all作为参考列的一个选项。但是,如果我改变主意并想稍后更改它nilify_all,那么在迁移内部有没有办法做到这一点?
创建表的迁移:
def change do
create table(:messages) do
add :body, :text
add :sender_id, references(:users, on_delete: :delete_all)
timestamps()
end
create index(:messages, [:sender_id])
end
Run Code Online (Sandbox Code Playgroud)
我正在寻找这样的东西:
def change do
change_options table(:messages), :user_id, on_delete: :nilify_all
end
Run Code Online (Sandbox Code Playgroud)
您可以modify为此使用,在 newon_delete中传递新选项type,但是我在尝试此操作时遇到了此错误,修复方法是首先手动DROP设置约束。您还需要同时指定 anup和 adown版本,因为它modify是不可逆的。
def up do
execute "ALTER TABLE posts DROP CONSTRAINT posts_user_id_fkey"
alter table(:posts) do
modify(:user_id, references(:users, on_delete: :delete_all))
end
end
def down do
execute "ALTER TABLE posts DROP CONSTRAINT posts_user_id_fkey"
alter table(:posts) do
modify(:user_id, references(:users, on_delete: :nothing))
end
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1437 次 |
| 最近记录: |