SkinnyGeek1010

SkinnyGeek1010

Issue updating foreign key (typo in migration key?)

I ran into an issue with wanted to update the foreign key on a record. When I try to do this I get an error. Based on the error i’m not sure if the original schema key is incorrectly named (note I used interest_list singular in the ecto part) or if it’s another problem. Also notice how the two tables are combined in the foreign key (may be normal though not sure).

What i’m trying to do it take an "Interests" record and change the id to a different existing "interest_lists" record.

I also wasn’t sure if updating a foreign key is disallowed. I’ve tried to update the key in the Postgres GUI and that works fine.


``` ** (Ecto.ConstraintError) constraint error when attempting to update struct:
     * foreign_key: interests_interest_list_id_fkey

 If you would like to convert this constraint into an error, please
 call foreign_key_constraint/3 in your changeset and define the proper
 constraint name. The changeset has not defined any constraint.

I have an `interest_lists` table that has many `interests` (one to many). However when I made the migration I mistyped the key... should have been `interest_lists_id` (lists with an s) I think?

current code:
<br>

defmodule Haven.Data.Repo.Migrations.CreateInterest do
@moduledoc “”"
Migration to add initial interests table
“”"
use Ecto.Migration

def change do
create table(:interests, primary_key: false) do
timestamps()
add :id, :uuid, primary_key: true
add :interest_list_id, references(:interest_lists, on_delete: :nothing, type: :binary_id)


defmodule Haven.Data.Repo.Migrations.CreateInterestList do
use Ecto.Migration

def change do
create table(:interest_lists, primary_key: false) do
timestamps()
add :id, :uuid, primary_key: true
add :name, :string
add :organization_id, :string
end
end
end


defmodule Haven.Data.InterestList do
use Haven.Data.Helpers, :schema

@primary_key {:id, :binary_id, autogenerate: true}
schema “interest_lists” do
field :name, :string
field :organization_id, :string
has_many :interests, Interests
timestamps()
end

@insert_params [:name, :organization_id]
@required_params [:name, :organization_id]

def changeset(struct, params \ %{}) do
struct
|> cast(params, @insert_params)
|> validate_required(@required_params)
end
end


defmodule Haven.Data.Schema.Interest do
use Haven.Data.Helpers, :schema
alias Haven.Data.Repo
alias Haven.Data.Schema.Interest

@required [:is_complete]

def update_completed_changeset(struct, params \ %{}) do
struct
|> cast(params, allowed_update_fields())
|> validate_required(@required)
end

@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema “interests” do
timestamps()
belongs_to :interest_list, Haven.Data.InterestList
# … more fields
end

defp allowed_update_fields do
[
:interest_list_id,
#.. more fields
]
end
end

Most Liked

OvermindDL1

OvermindDL1

This just means that your changeset function is missing the foreign_key_constraint/3 call in it, add one as appropriate on your interest list id. :slight_smile:

As for the ‘cause’ of that error, you tried to assign a value to a foreign key field (completely legitimate action), however the value you assigned to that field had no matching ID on the remote foreign table, I.E. no row existed with that id. :slight_smile:

SkinnyGeek1010

SkinnyGeek1010

Thanks a million @OvermindDL1 !

I did try to add that function a while back and saw errors: [interest_list_id: {"does not exist", []}] but I thought it was saying the key doesn’t exist. Makes sense now.

Also I was getting that error running my test and didn’t realize that indeed… none of the interest lists were in the test database … whoops.

:beers:

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
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
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

Other popular topics Top

axelson
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...
239 48475 226
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
bsollish-terakeet
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
nsuchy
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
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 127536 1222
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement