tnederlof
Removing an Association with Ecto
I have an Event that has_one Number (the schema and migrations are shown simplified below). I can use put_assoc when inserting a number to associate a number with an event. However I would like to be able to disassociate the two (essentially remove the relationship) without deleting the Number or Event (so the number would end up with no event_id). Any ideas would be quite helpful.
defmodule App.Events.Event do
use Ecto.Schema
import Ecto.Changeset
schema "events" do
field :description, :string
field :name, :string
has_one :number, App.Events.Number
timestamps()
end
@doc false
def changeset(event, attrs) do
required_fields = [:name]
optional_fields = [:description]
event
|> cast(attrs, required_fields ++ optional_fields)
|> validate_required(required_fields)
end
end
defmodule App.Repo.Migrations.CreateEvents do
use Ecto.Migration
def change do
create table(:events) do
add :name, :string
add :description, :text
timestamps()
end
end
end
defmodule App.Events.Number do
use Ecto.Schema
import Ecto.Changeset
schema "numbers" do
field :description, :string
field :name, :string
field :phone_number, :string
belongs_to :event, App.Events.Event
timestamps()
end
@doc false
def changeset(number, attrs) do
required_fields = [:name, :phone_number]
optional_fields = [:description]
number
|> cast(attrs, required_fields ++ optional_fields)
|> validate_required(required_fields)
end
end
defmodule App.Repo.Migrations.CreateNumbers do
use Ecto.Migration
def change do
create table(:numbers) do
add :name, :string
add :description, :text
add :phone_number, :string, null: false
add :event_id, references(:events, on_delete: :delete_all)
timestamps()
end
end
end
Most Liked
idi527
![]()
Would setting the event_id to NULL work?
import Ecto.Query
def disassociate_number_from_event(number_id) do
App.Events.Number
|> where(id: ^number_id)
|> App.Repo.update_all(set: [event_id: nil])
end
3
Popular in Questions
Hello, how can I check the Phoenix version ?
Thanks !
New
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone.
What strikes me is th...
New
I am trying to implement my new.html.eex file to create new posts on my website.
new.html.eex:
<h1>Create Post</h1>
<%= ...
New
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)?
Would
mix ecto.rollback -v 200809061...
New
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Using vs code and installed ElixirLS: support and debugger.
And I got an error popped up on start up says
Failed to run ‘elixir’ comma...
New
How to handle excepions in elixir?
Suppose i have A, B, C ,D, E modules. and each module has get() function.
A.get() method will call t...
New
Hi!
In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir?
Searched the docs for ip address and the web, no good results.
Thanks!
New
I have a User schema with a :from_id field set to type :string:
defmodule TweetBot.Repo.Migrations.CreateUsers do
use Ecto.Migration
...
New
What is the proper way to load a module from a file in to IEX?
In the python world, doing something like this pretty standard:
from ....
New
Other popular topics
Hello all!
I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
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
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Credo is smart enough to check for (something like) this:
assert length(the_list) == 0
with this response:
Checking if an enum is empt...
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. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!)
This post collects co...
New
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something…
Haskell reminds me of Java, and e...
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








