loon

loon

Ecto has_many :on_delete protect

Hi awesome people,

I have a User and Role table. Each role has many users.

Here is the table structure in ecto migration file.

    create table(:business_user, primary_key: false) do
      add :business_id, references(:businesses, on_delete: :delete_all), null: false
      add :user_id, references(:users, on_delete: :delete_all), null: false
      add :role_id, references(:role, on_delete: :delete_all), null: false

      timestamps()
    end

I know on_delete: :delete_all) will delete all related business_users if the role is deleted. But how to i protect this role from accidentally deleted?

Because i came from Django, it has on_delete=PROTECT to protect the role from deleted and error raised. I am new to ECTO, i not sure if there is anything I am missing here :slight_smile:

Marked As Solved

c4710n

c4710n

For you case, you should use on_delete: :restrict or on_delete: :nothing.

Why?

In short, read h Ecto.Migration.references.

In detail:

  • :on_delete option specifies the behavior of referencing rows when a referenced row is deleted.
  • When you call add :role_id, references(:role, on_delete: :delete_all), null: false, we say that business_user references role:
    • rows in business_user are the referencing rows.
    • rows in role are the referenced rows.
  • :on_delete has 4 possible values:
    • :delete_all
      • PostgreSQL clause: ON DELETE CASCADE
      • description: The referencing rows will be deleted.
    • :nilify_all
      • PostgreSQL clause: ON DELETE SET NULL
      • description: The foreign key of referencing rows will be set as NULL.
    • :restrict
      • PostgreSQL clause: ON DELETE RESTRICT
      • description: Prevent deletion of a referenced row.
    • :nothing (default behavior) ::
      • PostgreSQL clause: NO ACTION
      • description: Do nothing. (It will cause an error when checking constrains.)

You can try to read the book - Programming Ecto, it will clear all you confusions and give you new insights.

Also Liked

c4710n

c4710n

As supplementary:

  • above code just do data consistency, it is not helpful for user experience —— You shouldn’t just popup a model, and tell users “database error occurs”.
  • try to use Ecto.Changeset.no_assoc_constraint before deleting any referenced rows, :on_delete is just the last line of defense for data consistency.
dimitarvp

dimitarvp

Hey, don’t forget that part of the Repo config options that you can supply in your config/**.exs files (only those that pertain to actual DB connections, and they are a lot) are actually documented inside DBConnection.start_link/2. :003:

thomas.fortes

thomas.fortes

Isn’t django PROTECT an equivalent of SQL RESTRICT?

It’s been a while since I used django, but if so, on_delete: :restrict should do what you want.

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New

Other popular topics Top

9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 29703 241
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39523 209
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement