I am getting crazy. Now working a half day on my problem and can't find the solution/error. Creating a chat, I got a chat model and a chat_user model which has a user model as a foreign key.
Chat has has_many :chat_users, Test.Chat.ChatUser, foreign_key: :chat_id as has many, and the chat_user has belongs_to :user, Test.User and belongs_to :chat, Test.Chat.Chat.
I am preloading the associations with:
defp preload_associations(chat, params \\ nil) do
Repo.preload(chat, [
:chat_users
])
end
The Error i get is: ** (Postgrex.Error) ERROR 42703 (undefined_column): column c0.user_id does not exist
The table chat_users in the database has the field user_id with a foreign key. It seems to me, Ecto is searching for user_id in chat instead of chat_user.
Any idea what I am doing wrong? Thanks.
EDIT: Models
defmodule Test.Chat.ChatItem do
@moduledoc false
use Test.Web, :model
schema "chats" do
field :name, :string
...
has_many :chat_users, Test.Chat.ChatUser, foreign_key: :chat_id
timestamps()
end
defmodule Test.Chat.ChatUser do
use Test.Web, :model
schema "chat_users" do
...
belongs_to :user, Test.User
belongs_to :chat, Test.Chat.Chat
end
And
defmodule Test.User do
...
foreign_key: :chat_idfromhas_manydefinition. - denis.peplin* (Ecto.QueryError) deps/ecto/lib/ecto/association.ex:495: field Test.Chat.ChatUser.chat_item_id in where does not exist in the schema in query. And yes. I want to preload chat_users, the next step is to preload users from chat_users. chat_users has fields I need and users also. REST-Api, need all the stuff at once in the response. - Sardoan