ERROR 42830 (invalid_foreign_key) there is no unique constraint matching given keys for referenced table "course_sections"

I am using the auto generated primary key for the following migrations but it could not work,
What wrong did I make
the parent table migration is :

defmodule Keen.Repo.Migrations.CreateCourseSections do

  use Ecto.Migration

  def change do

    create table(:course_sections) do

      add :description, :string

      add(:course_id, references(:courses, on_delete: :delete_all), primary_key: true)

      timestamps()

    end

  end

end

The child table is

defmodule Keen.Repo.Migrations.CreateCourseSectionFiles do
  use Ecto.Migration

  def change do
    create table(:course_section_files) do
      add :name, :string
      add :uri, :string

      add(:course_section_id, references(:course_sections, on_delete: :delete_all),
        primary_key: true
      )

      timestamps()
    end
  end
end

Why is the course_id a primary key? Aren’t there multiple course sections for a given course? Same with the course_section_files. You don’t need to make something a primary key just to have it be a foreign key

2 Likes

Thanks, i removed all primary_key: true instances and migration worked