Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

elixir

ecto

The Ecto documentation describes the options available to references/2, but does not document what those options do. The options available are:

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

What do they do?

like image 548
vaer-k Avatar asked Aug 06 '19 22:08

vaer-k


People also ask

What does ecto migrate do?

Migration behaviour (Ecto SQL v3. 8.3) Migrations are used to modify your database schema over time. This module provides many helpers for migrating the database, allowing developers to use Elixir to alter their storage in a way that is database independent.

What is ecto elixir?

Ecto is an official Elixir project providing a database wrapper and integrated query language. With Ecto we're able to create migrations, define schemas, insert and update records, and query them. Changesets. In order to insert, update or delete data from the database, Ecto. Repo.


1 Answers

This is actually a SQL question at root.

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

The PostgreSQL documentation outlines these options clearly:

  • :nothing - if any referencing rows still exist when the constraint is checked, an error is raised; this is the default behavior if you do not specify anything.
  • :delete_all - specifies that when a referenced row is deleted, row(s) referencing it should be automatically deleted as well
  • :nilify_all - causes the referencing column(s) in the referencing row(s) to be set to nil when the referenced row is deleted
  • :restrict - prevents deletion of a referenced row. It will fail if there is a referenced object.

:nothing and :restrict are similar but:

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.

like image 88
Justin Wood Avatar answered Sep 30 '22 23:09

Justin Wood