Cast_assoc invalid

Hi all
I have following schemas

schema "languages_code" do
   field :code, :string
   field :text, :string

    timestamps
end

and another schema that will be associated with languages_code schema:

schema "countries" do

    belongs_to :code, CountryCode, references: :alpha2
    belongs_to :language, LanguageCode, references: :code
    field :text, :string

    timestamps

  end

  def changeset(model, params \\ %{}) do

	  model
	  |> cast(params, [:text])
	  |> cast_assoc(:code)
	  |> cast_assoc(:language)
	  |> validate_required([:code, :language, :text])

  end

Then I test it as follow:

iex(1)> alias Busiket.Country
Busiket.Country
iex(2)> v = %{code: "CH", language: "DE", text: "Schweiz"}
%{code: "CH", language: "DE", text: "Schweiz"}
iex(3)> c = Country.changeset(%Country{}, v)
#Ecto.Changeset<action: nil, changes: %{text: "Schweiz"},
 errors: [language: {"is invalid", [type: :map]},
  code: {"is invalid", [type: :map]}], data: #Busiket.Country<>, valid?: false>
iex(4)>  

What is wrong with association?

Update
I tried as follow:

iex(4)> v = %{code: %{code: "CH"}, language: %{alpha2: "DE"}, text: "Schweiz"}
%{code: %{code: "CH"}, language: %{alpha2: "DE"}, text: "Schweiz"}
iex(5)> c = Country.changeset(%Country{}, v)                                  
#Ecto.Changeset<action: nil,
 changes: %{code: #Ecto.Changeset<action: :insert, changes: %{},
    errors: [alpha2: {"can't be blank", []}, alpha3: {"can't be blank", []}],
    data: #Busiket.CountryCode<>, valid?: false>,
   language: #Ecto.Changeset<action: :insert, changes: %{},
    errors: [code: {"can't be blank", []}, text: {"can't be blank", []}],
    data: #Busiket.LanguageCode<>, valid?: false>, text: "Schweiz"}, errors: [],
 data: #Busiket.Country<>, valid?: false>

I’ve forgot to mention, that the data on the language_code table is already available:

I do not have to insert it, only to validate, if the value of the field code of the country is match to the table of language_code.

Thanks

1 Like

If you don’t need to insert it, just validate it exists, you can let the database take care of it for you:

schema "countries" do
   field :code
   field :language
   field :text
   timestamps
end

def changeset(struct, params \\ %{}) do
  struct
  |> cast(params, [:text, :code, :language])
  |> validate_required([:code, :language, :text])
  |> foreign_key_constraint(:code, name: :name_of_the_code_foreign_key)
  |> foreign_key_constraint(:language, name: :name_of_the_language_foreign_key)
end

Then you can query your database to find the name of the foreign keys. If you are not sure the foreign key exists, then you can try to insert a country with a code that certainly does not exist and Ecto will error, telling you which foreign key name that failed, or succeed, which means you have no primary keys and therefore you should add one.

2 Likes

First of all, thanks for your answer.

Do I need anymore?

|> cast_assoc(:code)
|> cast_assoc(:language)

or just:

def changeset(struct, params \\ %{}) do
  struct
  |> cast(params, [:text])
  |> validate_required([:code, :language, :text])
  |> foreign_key_constraint(:code, name: :name_of_the_code_foreign_key)
  |> foreign_key_constraint(:language, name: :name_of_the_language_foreign_key)
end

Thanks

Sorry, you are right. You don’t need cast_assoc but make sure you pass both :code and :language to cast. I will edit my previous answer for clarify.

1 Like

Thanks jose.

Did it work? :smiley:

Sorry.
I changed my schema to:

schema "countries" do

    field :code, :string
    field :language, :string
    field :text, :string

    timestamps

  end

  def changeset(model, params \\ %{}) do

	  model
	  |> cast(params, [:code, :language, :text])
	  |> validate_required([:code, :language, :text])
	  |> foreign_key_constraint(:code, name: :alpha2)
      |> foreign_key_constraint(:language, name: :code)

  end

as output

iex(11)> v = %{code: "LK", language: "ZZ", text: "Schweiz"}
%{code: "LK", language: "ZZ", text: "Schweiz"}
iex(12)> c = Country.changeset(%Country{}, v)              
#Ecto.Changeset<action: nil,
 changes: %{code: "LK", language: "ZZ", text: "Schweiz"}, errors: [],
 data: #Busiket.Country<>, valid?: true>

It should complain, a country with code LK does not exists!

Thanks

@josevalim I found the error, it is on migration file, since I rename the column:

create table(:countries) do
  add :coun, references(:countries_code, column: :alpha2, type: :string)
  add :lang, references(:languages_code, column: :code, type: :string)
  add :text, :string

  timestamps
		end

create unique_index(:countries, [:coun, :lang])

I rename the column from coun to code and from lang to language.
Now how to add references to the new name of columns?

Delete and create the countries database with:

  def change do

    drop table(:countries)

    create table(:countries) do
      add :code, references(:countries_code, column: :alpha2, type: :string)
      add :language, references(:languages_code, column: :code, type: :string)
      add :text, :string

      timestamps
    end

    create unique_index(:countries, [:code, :language])

  end

and tried with:

iex(2)> v = %{code: "LK", language: "ZZ", text: "Schweiz"}
%{code: "LK", language: "ZZ", text: "Schweiz"}
iex(3)> c = Country.changeset(%Country{}, v)  
#Ecto.Changeset<action: nil,
 changes: %{code: "LK", language: "ZZ", text: "Schweiz"}, errors: [],
 data: #Busiket.Country<>, valid?: true>

The compiler should complain.

If you are using “references”, one is automatically added for you. Please
check the answer in my first post, I think you did not add the new fields
to the “cast” call in your changeset function (because I forgot to do so as
well in my original reply).

Hi jose
I tried again as you metioned and it does not work.

The schema and changeset function:

  schema "countries" do

    field :iso_country, :string
    field :iso_language, :string
    field :name, :string

    timestamps

  end

  def changeset(struct, params \\ %{}) do

    struct
    |> cast(params, [:iso_country, :iso_language, :name])
    |> validate_required([:iso_country, :iso_language, :name])
    |> foreign_key_constraint(:iso_country, name: :iso)
    |> foreign_key_constraint(:iso_language, name: :iso)

  end

and migration:

def change do

create table(:countries) do

  add :iso_country, references(:countries_code, column: :iso, type: :string)
  add :iso_language, references(:languages_code, column: :iso, type: :string)
  add :name, :string

  timestamps
end

create unique_index(:countries, [:iso_country, :iso_language])

When I look at the pgAdmin console, the migration did the right job:

as you can see on the picture, the constraints on countries table is available.

I test as follow:

iex(4)> a = %{iso_country: "YY", iso_language: "DE", name: "Schweiz"}
%{iso_country: "YY", iso_language: "DE", name: "Schweiz"}
iex(5)> Country.changeset(%Country{}, a)
#Ecto.Changeset<action: nil,
 changes: %{iso_country: "YY", iso_language: "DE", name: "Schweiz"}, errors: [],
 data: #Busiket.Country<>, valid?: true>

The country with iso YY does not exists on countries_code db:

What am I doing wrong?

Thanks.

Constraints are only tested after you do a repository operation, such as Repo.insert, since it is the database reporting what is wrong (rather than we testing it).

2 Likes

So, I have to do Repo.insert to get to know if there is a constraint error or not?

1 Like

Yes, that’s by definition how constraints in Ecto work. They rely on the
database as the da abase is the only one who can effectively say if that
relationship exists or not.

2 Likes

My last question(hopefully :slight_smile: ), when I tried to insert into db, I’ve got an exception:

iex(5)> e = Repo.insert(c)
[debug] QUERY ERROR db=16.0ms
INSERT INTO "countries" ("iso_country","iso_language","name","inserted_at","updated_at") VALUES ($1,$2,$3,$4,$5) RETURNING "id" ["YY", "DE", "Schweiz", {{2016, 11, 24}, {13, 28, 8, 0}}, {{2016, 11, 24}, {13, 28, 8, 0}}]
** (Ecto.ConstraintError) constraint error when attempting to insert struct:

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 defined the following constraints:

How to get a message instead raised exception?
In doc https://hexdocs.pm/ecto/Ecto.Repo.html#c:insert/2 it says, I should get a tuple.

Thanks

Can you post the full message? The error message is telling exactly what you need to do. You need to pass the :name option to foreign_key_constraint/3 with the foreign key name that is failing.

I’ve found the solution:

  def changeset(struct, params \\ %{}) do

    struct
    |> cast(params, [:iso_country, :iso_language, :name])
    |> validate_required([:iso_country, :iso_language, :name])
    |> foreign_key_constraint(:iso_country)
    |> foreign_key_constraint(:iso_language)

  end

It works as expected.
At last, what is the :name options for, that I can pass?

with the name option, I’ve got error as I mentioned above.

So I will post the final code, that works.

  schema "countries" do

    field :iso_country, :string
    field :iso_language, :string
    field :name, :string

    timestamps

  end

  def changeset(struct, params \\ %{}) do

    struct
    |> cast(params, [:iso_country, :iso_language, :name])
    |> validate_required([:iso_country, :iso_language, :name])
    |> foreign_key_constraint(:iso_country)
    |> foreign_key_constraint(:iso_language)

  end

Look at the function foreign_key_constraint, I do not pass the name option parameter and it works. Why? What is the use of the name option parameter?

The name option would be given to the foreign_key_constraint function. If it works without passing the :name option, it means Ecto is guessing the proper name for you.