benswift

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

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
Post #7
tomkonidas

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?:thinking:).
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

Where Next?

Popular in Questions Top

marius95
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
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
shahryarjb
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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
vac
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
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
Emily
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
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
vegabook
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
dokuzbir
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 Top

AstonJ
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
siddhant3030
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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
pmjoe
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
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
belgoros
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
jason.o
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
komlanvi
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
AstonJ
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement