benswift
How can I modify the on_delete behaviour of a fk reference in a migration
I created a table with a foreign key, and specified on_delete: :nothing like so:
create table(:foo) do
add :bar_id, references(:bar, on_delete: :nothing)
timestamps()
end
I now want to change the on_delete behaviour to be :delete_all. I tried doing alter table + modify :bar_id… in a new migration, but it gave me an “already exists” error.
Any ideas?
Most Liked
talkignquickly
For anyone coming here via Google, something like this:
alter table("comments") do
modify :post_id, references(:posts, on_delete: :delete_all),
from: references(:posts, on_delete: :nothing)
end
Now works as per example here in docs. Not sure exactly when this changed but hopefully saves someone some time. Tested with Ecto 3.10.3
14
tomkonidas
yea so you would probably want to drop the fk. you will need to set up/down however because ecto wont know how to rollback. So
def up do
drop constraint(:foo, "foo_bar_id_fkey")
alter table(:foo) do
modify :bar_id, references(:bar, on_delete: :delete_all)
end
end
def down do
drop constraint(:foo, "foo_bar_id_fkey")
alter table(:foo) do
modify :bar_id, references(:bar, on_delete: :nothing)
end
end
I am surprised this does not work (unless it does?
).
This would save you writing up and down
def change do
drop constraint(:foo, "foo_bar_id_fkey")
alter table(:foo) do
modify :bar_id, references(:bar, on_delete: :delete_all), from: references(:bar, on_delete: :nothing)
end
end
3
Popular in Questions
Hello everyone,
I try to use an Javascript Event Handler in my root.html.leex file.
Therefore I created a function in the app.js file: ...
New
Hello, how can I check the Phoenix version ?
Thanks !
New
Hello, I have map which I want to convert it to string like this:
the map:
%{last_name: "tavakkoli", name: "shahryar"}
the string I ne...
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set?
Thanks.
New
Hi,
I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors:
[WARN] - (starship::utils): Executing command ...
New
I have VueJS GUIs with the project generated using Webpack.
I have Elixir modules that will need to be used by the VueJS GUIs.
I forese...
New
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
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
Other popular topics
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
Hi,
I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set?
Thanks.
New
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
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
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
Hi everyone,
I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including.
What is Phoenix LiveV...
New
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
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
- #performance









