私は狂っています。今私の問題で半日働いて、解決策/エラーを見つけることができません。チャットを作成すると、チャットモデルとチャットモデルが得られます。このモデルには、ユーザーモデルが外部キーとして用意されています。エリクシール/フェニックスエクトプリロードアソシエーションがhas_manyで動作しない
チャットはhas_many :chat_users, Test.Chat.ChatUser, foreign_key: :chat_id
と多くあり、chat_userはbelongs_to :user, Test.User
とbelongs_to :chat, Test.Chat.Chat
です。
私はとの関連付けをプリロードしています:
defp preload_associations(chat, params \\ nil) do
Repo.preload(chat, [
:chat_users
])
end
私が手にエラーがある:** (Postgrex.Error) ERROR 42703 (undefined_column): column c0.user_id does not exist
表chat_users
データベースには、外部キーでフィールドuser_id
を持っています。私のように、Ectoはchat_user
の代わりにuser_id
をチャットで探しています。
私が間違っていることは何ですか?ありがとう。
EDIT:モデル
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
そして
defmodule Test.User do
...
あなたのモデルをここに掲載したほうがいいです。モデルのエラーをインワードの説明よりも見つけやすくなります。 –
'foreign_key :: chat_id'を' has_many'定義から削除してみてください。 –
そして...あなたはchat_usersをあらかじめロードしたいのですか、代わりにユーザーをプリロードしたいのですか? –