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

What do the on_delete options in Ecto.Migrations.references/2 do?

Ecto documentation 描述了 references/2 可用的选项,但没有记录这些选项的作用。可用的选项是:

他们是做什么的?

这实际上是一个 SQL 根问题。

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

PostgreSQL documentation 清楚地概述了这些选项:

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

:nothing:restrict 相似但是:

the essential difference between these two choices is that [:nothing] allows the check to be deferred until later in the transaction, whereas [:restrict] does not.