I'm using Ecto 2.0 and trying to run this query:
query = from e in EmpireInstance,
where: e.user_id == ^user.id,
preload: [:board]
the schema for board looks like this:
schema "board_instances" do
belongs_to :empire, PlexServer.EmpireInstance
has_many :board_pieces, BoardTileInstance
timestamps
end
EmpireInstance schema:
schema "empire_instances" do
...
belongs_to :user, PlexServer.User
has_one :board, PlexServer.BoardInstance
...
timestamps
end
I'm getting this error:
** (Ecto.QueryError) deps/ecto/lib/ecto/association.ex:392: field
PlexServer.BoardInstance.empire_instance_idinwheredoes not exist in the schema in query:from b in PlexServer.BoardInstance, where: b.empire_instance_id in ^[1], select: {b.empire_instance_id, b}
It looks like it's still trying to use the default belongs_to id of module_name + _id. Is there way to solve this, besides changing the belongs_to atom back to default?
Here's the code where the query is made:
def show(conn, _) do
user = Guardian.Plug.current_resource(conn)
query = from e in EmpireInstance,
where: e.user_id == ^user.id,
preload: [:board]
case Repo.one(query) do
nil ->
conn
|> put_status(:unprocessable_entity)
|> json(%{errors: ["No associated empire"]})
empire ->
render(conn, PlexServer.EmpireInstanceView, "show.json", empire: empire)
end
end
EmpireInstance? - Dogbertempire_instance_idwhenbelongs_to's documentation implies it should use association name +_id, soempire_id. What happens if you add, foreign_key: :empire_idafterbelongs_to :empire, PlexServer.EmpireInstance? - Dogbert