'preload' results in 'Complex :in queries is not supported' error
Opened this issue · 2 comments
When using an Ecto.Query.preload(:relation_name)
in a pipeline with a query, the following error is thrown:
Complex :in queries is not supported by the Mnesia adapter.
This error is not indicative of what is happening here, so:
a) we need a better error.
b) maybe it is possible to support this feature after all?
@Qqwy can you contribute a test that reproduces it? We do support some :in
queries, but they are emulated by nested :or
's and don't work in all possible cases.
Of course! I am building a chat application, and this is what I am doing:
Schemas:
defmodule MyApp.Chat.User do
use Ecto.Schema
import Ecto.Changeset
schema "users" do
belongs_to :app, MyApp.Chat.App
field :name, :string
has_many :sent_messages, MyApp.Chat.Message
timestamps()
end
end
defmodule MyApp.Chat.Message do
use Ecto.Schema
import Ecto.Changeset
schema "message" do
belongs_to :sender, MyApp.Chat.User
belongs_to :conversation, MyApp.Chat.Conversation
field :content, :string
timestamps()
end
end
Migrations:
defmodule MyApp.Repo.Migrations.CreateUsers do
use Ecto.Migration
def change do
create table(:users) do
add :name, :string
add :app_id, :integer #references("apps")
timestamps()
end
end
end
defmodule MyApp.Repo.Migrations.CreateMessage do
use Ecto.Migration
def change do
create table(:message) do
add :sender_id, :integer # references("users")
add :conversation_id, :integer # references("conversations")
add :content, :string
timestamps()
end
end
end
(For this example, the 'conversation' schema and 'apps' schema are not really important)
I then attempt the following query:
from(MyApp.Chat.Message, where: [conversation_id: ^conversation_id], order_by: [desc: :inserted_at], limit: 10)
|> preload(:sender)
|> MyApp.Repo.all()
to fetch all Messages which should each contain there respective sender.
Executing this query fails with the error shown above (Complex :in queries is not supported by the Mnesia adapter.
).
I am not entirely sure how to extract a test out of this, but hopefully it does give you some indication of what is going on. (I try to run Ecto.Query.preload
on a field that is a belongs_to
in my schema).