I am trying to create a field user_scope_id
inside course_enrollment
that joins 2 tables through a composite foreign key:
create table("user_scope", primary_key: false) do
add(:user_id, references(:user, on_delete: :delete_all), primary_key: true)
add(:scope_id, references(:scope, on_delete: :delete_all), primary_key: true)
end
create(unique_index(:user_scope, [:user_id, :scope_id]))
create table("course_enrollment", primary_key: false) do
add(:user_id, references(:user, on_delete: :delete_all), primary_key: true)
add(:scope_id, references(:scope, on_delete: :delete_all), primary_key: true)
add(:course_id, references(:course, on_delete: :delete_all), primary_key: true)
add(:user_scope_id, references(:user_scope, with: [user_id: :user_id, scope_id: :scope_id], on_delete: :delete_all), null: false)
add(:active, :boolean, default: true)
timestamps()
end
create(unique_index(:course_enrollment, [:course_id, :user_id, :scope_id]))
However, since the primary key in user_scope
is composite, the user_scope_id
association inside course_enrollment is failing to find a column named “id” as per the error that I’m getting:
I found that there exists a :column
attribute inside references
but I don’t know if that’s of any use:
https://hexdocs.pm/ecto_sql/Ecto.Migration.html#references/2-options
I just want to be able to reference user_scope from course_enrollment, though maybe it’s not worth it to create the foreign key in the database since I could just join it through the queries.
Any insight on this?