Composite Primary Keys using Elixir Ecto
Ecto allows us to map a table with composite primary keys into our schemas. The way to do that is by using the primary_key: true option for the Ecto.Schema.field/3 instead of dealing with the special module attr @primary_key. Check this out:
defmodule MyApp.OrderItem do
use Ecto.Schema
@primary_key false
schema "order_items" do
field :product_id, :integer, primary_key: true
field :order_id, :integer, primary_key: true
field :quantity, :integer
end
end
Tweet