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

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:

1 Like

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:

1 Like

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