Ecto Migration On Update Cascade

Hi!

When setting up a migration, I need to specify the foreign key constraint, ON UPDATE -> CASCADE (MySQL). I’ve read the documentation, whoever there is only mention to the on_delete constraint. Using, on_replace, or on_cascade doesn’t work. Is this method implemented?

  def change do
    create table(:user_exam) do
      add :user_id, references(:user, on_delete: :nothing, on_cascade: :update), null: false
      add :exam_id, references(:exam, on_delete: :nothing, on_cascade: :update), null: false
      add :level, :integer, null: false

      timestamps()
    end

    create unique_index(:user_exam, [:user_id, :exam_id])
    create index(:user_exam, [:user_id])
    create index(:user_exam, [:exam_id])
  end

Doesn’t the option on_update: :update_all do what you want?
https://hexdocs.pm/ecto_sql/Ecto.Migration.html#references/2

1 Like

@thojanssens1 Thanks! I couldn’t find it.

If I understand correctly, if the referenced ID is updated, you want all references to it to update as well.

Can I ask you how an ID can ever change?

In most cases, this is not necessary. But we had cases in which we need to update an ID for an specific table, in order to comply with a client