Ecto.Migrations.references/2 中的 on_delete 选项有什么作用?

2024-03-27

The 埃克托文档 https://hexdocs.pm/ecto_sql/Ecto.Migration.html#references/2描述了可用的选项references/2,但没有记录这些选项的作用。可用的选项有:

  • :nothing
  • :delete_all
  • :nilify_all
  • :restrict

他们在做什么?


这实际上是一个 SQL 问题。

https://github.com/elixir-ecto/ecto_sql/blob/52f9d27a7ad86442f442bad2f7ebd19ba09ddc61/lib/ecto/adapters/myxql/connection.ex#L902-L905 https://github.com/elixir-ecto/ecto_sql/blob/52f9d27a7ad86442f442bad2f7ebd19ba09ddc61/lib/ecto/adapters/myxql/connection.ex#L902-L905

The PostgreSQL 文档 https://www.postgresql.org/docs/9.5/ddl-constraints.html清楚地概述了这些选项:

  • :nothing- 如果检查约束时仍然存在任何引用行,则会引发错误;如果您未指定任何内容,这是默认行为。
  • :delete_all- 指定当删除引用的行时,引用它的行也应自动删除
  • :nilify_all- 导致引用行中的引用列设置为nil当引用的行被删除时
  • :restrict- 防止删除引用的行。如果存在引用的对象,则会失败。

:nothing and :restrict相似但是:

这两种选择的本质区别在于 [:nothing] 允许将检查推迟到稍后的时间 交易,而 [:restrict] 才不是。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Ecto.Migrations.references/2 中的 on_delete 选项有什么作用? 的相关文章

随机推荐