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 ![]()
Marked As Solved
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_deleteoption 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 thatbusiness_userreferencesrole:- rows in
business_userare the referencing rows. - rows in
roleare the referenced rows.
- rows in
:on_deletehas 4 possible values::delete_all- PostgreSQL clause:
ON DELETE CASCADE - description: The referencing rows will be deleted.
- PostgreSQL clause:
:nilify_all- PostgreSQL clause:
ON DELETE SET NULL - description: The foreign key of referencing rows will be set as
NULL.
- PostgreSQL clause:
:restrict- PostgreSQL clause:
ON DELETE RESTRICT - description: Prevent deletion of a referenced row.
- PostgreSQL clause:
:nothing(default behavior) ::- PostgreSQL clause:
NO ACTION - description: Do nothing. (It will cause an error when checking constrains.)
- PostgreSQL clause:
You can try to read the book - Programming Ecto, it will clear all you confusions and give you new insights.
Also Liked
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_constraintbefore deleting any referenced rows,:on_deleteis just the last line of defense for data consistency.
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. ![]()
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.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









