Composite foreign keys in ecto

I made two fields in my schema primary keys. The schema is this:

@primary_key false
schema "actions" do
  field(:level, :integer)
  field(:is_active, :boolean, default: true)
  field(:path, {:array, :string}, default: [], primary_key: true)
  field(:request, :string, primary_key: true)
  field(:name, :string)
  field(:description, :string)
many_to_many(:roles, Example.Role, join_through: "roles_actions")

This is role actions schema

   @primary_key false
  schema "roles_actions" do
    belongs_to(:role, Example.Role, primary_key: true, type: :string)
    belongs_to(:action, Example.Action, primary_key: true)


The migration for role_actions:

       def change do
        create table(:roles_actions, primary_key: false) do
       add(:action_id, references(:actions), null: false, primary_key: true)

       add(:role_id, references(:roles, column: :id, type: :string), null: false, primary_key: true)

How can I change the migration to add both of these fields in the role_actions action_id. How can I change my schema of actions to pass both of these composite keys in role actions and tell role_actions schema to use these two keys as composite in the action_id?

1 Like