Foreign key constraint does not exist

I am using uuid in my chapter migrationns and whenn i refrence that in lesson it gives me error

lesson migration

add :chapter_response_category_uuid, references(:chapter_response_categories, type: :uuid)

chapter migration

 create table(:chapter_response_categories, primary_key: false) do
      add :uuid, :binary_id, primary_key: true

errer

create table lesson
** (Postgrex.Error) ERROR 42703 (undefined_column) column "id" referenced in foreign key constraint does not exist

This might help: Ecto.Schema — Ecto v3.11.1 (not 100% certain).

I am studying database relationships with Ecto at the moment and I recall reading about the foreign key. The following is an excerpt from that page:

:foreign_key - Sets the foreign key field name, defaults to the name of the association suffixed by _id . For example, belongs_to :company will define foreign key of :company_id . The associated has_one or has_many field in the other schema should also have its :foreign_key option set with the same value.

Perhaps you should change this:

add :chapter_response_category_uuid, references(:chapter_response_categories, type: :uuid)

to this?

add :chapter_response_category_uuid, references(:chapter_response_categories, type: :uuid, foreign_key: :uuid)

If the issue is still there try this

add :chapter_response_category_uuid, references(:chapter_response_categories, type: :uuid, column: :uuid)
1 Like