我有两个表devices
和devices_type
,当删除devices.type_id
中的原始行时,我想在devices_types
中设置null。
#MyApp.Repo.Migrations.Devices
create table(:devices) do
...
add :type_id, references(:device_type, on_delete: :nothing)
...
end
#MyApp.Repo.Migrations.DeviceType
create table(:device_type) do
add :name, :string, comment: "Device type name"
end
参考文档references(table, opts \ []),我使用on_delete: :nilify_all
选项
#MyApp.Repo.Migrations.Devices
create table(:devices) do
...
add :type_id, references(:device_type, on_delete: :nilify_all)
...
end
另请参见,签入选项:https://hexdocs.pm/ecto/Ecto.Schema.html#has_many/3