当删除引用表行时如何设置null,Ecto.Migration引用/ 2

问题描述 投票:0回答:1

我有两个表devicesdevices_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
postgresql elixir ecto
1个回答
0
投票

参考文档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

© www.soinside.com 2019 - 2024. All rights reserved.