SkinnyGeek1010

SkinnyGeek1010

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

Showing Posts 1 to 3

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 OP

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:

OvermindDL1

OvermindDL1

Lol, yeah we’ve all done that. ^.^;

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
apz
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews