1stSolo

1stSolo

#ecto #ecto-migrations Reversible `references()` migration with from: option

how do I make a reversible reference migration, i.e. adding the referential constraint on “up” action and dropping it on “down” action, using from: keyword?
I’ve a pretty big migration and it’s much easier to use “def change” than “def up” and “def down”.

The documentation for references/3 says it can be used like such:

# Modify the :on_delete option of an existing foreign key
alter table("comments") do
  modify :post_id, references(:posts, on_delete: :delete_all),
    from: references(:posts, on_delete: :nothing)
end

which, I assume, would drop and recreate the constraint and is safe to use in “change” mode. How do I write one where “from” is a “no FK constraint” state?

Marked As Solved

jswanner

jswanner

Without the actual code that you are using it’s hard to give better help than pointing to the docs you’ve already quoted.

There’s also the option to use Ecto.Migration — Ecto SQL v3.14.0, which wouldn’t require you to break up your lengthy migration and provide yourself the means to define the reversal needed.

Also Liked

dimitarvp

dimitarvp

I applaud you for your curiosity. In my case when I stumble upon this I just write the up and down parts of the migration manually and move on.

I’d be inclined to think that’s the more likely outcome, yes.

Last Post!

dimitarvp

dimitarvp

I applaud you for your curiosity. In my case when I stumble upon this I just write the up and down parts of the migration manually and move on.

I’d be inclined to think that’s the more likely outcome, yes.

Where Next?

Popular in Questions Top

RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
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
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44139 214
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42533 114
New

We're in Beta

About us Mission Statement