Ecto Repo.all call when using group primary key fails

I’m trying to make a simple listing query for a schema looking like this:

defmodule BasicChannel do
use Chat.web, :model

schema "basic_channels" do
    belongs_to :family, Family, [primary_key: true]
    belongs_to :user, User, [primary_key: true]
    belongs_to :group_channel, Chat.GroupChannel
end

end

But when I try to load all BasicChannels Ecto fails complaining about ‘id’ missing.

Repo.all(Chat.BasicChannel) 
[debug] QUERY ERROR source="basic_channels" db=1.2ms
SELECT s0."id", s0."family_id", s0."user_id", s0."group_channel_id" FROM "basic_channels" AS s0 []
** (Postgrex.Error) ERROR 42703 (undefined_column): column s0.id does not exist

How can I tell Ecto that I don’t have a id field, and that the primary key is the group key (family_id, user_id)?

As often is the case, i found the answer to my own question.
I had to include @primary_key false just before the schema macro

@primary_key false
schema "basic_channels" do
    belongs_to :family, Family, [primary_key: true]
    belongs_to :user, User, [primary_key: true]
    belongs_to :group_channel, Chat.GroupChannel
end

And after that it works like a charm.

3 Likes

I’m glad you were able to solve it. Thanks for sharing and welcome to the forum!