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
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
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
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.
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
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex










