-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
No function clause matching in anonymous fn/1 in Ecto.Adapters.SQLite3.Connection.group_by/2 #160
Comments
👋 @waseigo Would you or @brownerd be able to prepare a minimal example that reproduces the issue? I ran the following on a Mac and it seems to work.iex(1)> Mix.install [:ecto_sqlite3]
iex(2)> defmodule Customer do
...(2)> use Ecto.Schema
...(2)>
...(2)> schema "customers" do
...(2)> field :name, :string
...(2)> end
...(2)> end
iex(3)> defmodule Order do
...(3)> use Ecto.Schema
...(3)>
...(3)> schema "orders" do
...(3)> belongs_to :customer, Customer
...(3)> end
...(3)> end
iex(4)> defmodule Repo do
...(4)> use Ecto.Repo, adapter: Ecto.Adapters.SQLite3, otp_app: :demo
...(4)> end
iex(5)> Application.put_env(:demo, Repo, database: "demo.db")
iex(6)> Repo.start_link
iex(7)> Repo.query("create table customers (id integer primary key, name text)")
iex(8)> Repo.query("create table orders (id integer primary key, customer_id integer references customers(id))")
iex(9)> Repo.insert_all("customers", [[name: "Ernst Handel"], [name: "QUICK-Stop"], [name: "Rattlesnake Canyon Grocery"]])
iex(10)> Repo.insert_all("orders", [[customer_id: 1], [customer_id: 1], [customer_id: 2]])
iex(11)> defmodule Customer do
...(11)> use Ecto.Schema
...(11)> schema "customers" do
...(11)> field :name, :string
...(11)> has_many :orders, Order
...(11)> end
...(11)> end
iex(12) import Ecto.Query
iex(13)> Customer |> join(:inner, [c], o in assoc(c, :orders)) |> group_by([c, o], c.id) |> select([c, o], %{id: c.id, name: c.name, num_orders: count(o.id)}) |> order_by([c, o], desc: count(o.id)) |> limit(^5) |> Repo.all
# [debug] QUERY OK source="customers" db=0.2ms queue=0.2ms idle=1634.4ms
# SELECT c0."id", c0."name", count(o1."id") FROM "customers" AS c0 INNER JOIN "orders" AS o1 ON o1."customer_id" = c0."id" GROUP BY c0."id" ORDER BY count(o1."id") DESC LIMIT ? [5]
[
%{id: 1, name: "Ernst Handel", num_orders: 2},
%{id: 2, name: "QUICK-Stop", num_orders: 1}
] |
The error itself is weird, it seems to happen at
|
Hello @ruslandoga, thanks for helping out. What we found out since yesterday: @brownerd can run the query just fine from within the I have uploaded the latest version of the SQLite3 db here: https://www.dropbox.com/scl/fi/6dnznvup0acm7rmz6o76f/northwind_elixir_traders_repo.db?rlkey=z3k7aif88cxd37m0kbyrk06h4&st=noqsepx6&dl=0 Do you think I should report this issue to the exqlite repo instead? |
I don't think the binary matters, at least with the information provided so far. The error happens during SQL "building" stage. |
@brownerd killed all the deps and re-installed. Upon running iex, he saw this warning during the build:
|
Could you please try removing |
Or even better, upload the whole project together with |
|
I was almost right :) The issue is renamed structs in Ecto. In the SQLite adapter you are using (ecto_sqlite3 0.15.1) the expected struct is But in Ecto 3.12+ it's been renamed to The quickfix is to update the adapter to the latest version. And the real fix is for this adapter to be more strict with the allowed Ecto versions :) Related: |
I tried the example above and I got this error Customer |> join(:inner, [c], o in assoc(c, :orders)) |> group_by([c, o], c.id) |> select([c, o], %{id: c.id, name: c.name, num_orders: count(o.id)}) |> order_by([c, o], desc: count(o.id)) |> limit(^5) |> Repo.all error: undefined function limit/2 (there is no such import) (╯°□°)╯** (CompileError) cannot compile code (errors have been logged) |
You need to run It doesn't matter much though, since I think it's clear what the issue is now: #160 (comment) |
Also I don't think that the pin operator is needed in front of "5", but doesn't hurt. |
Gotcha. The example ran fine 09:03:40.015 [debug] QUERY OK source="customers" db=0.2ms queue=0.2ms idle=1534.8ms [ |
The query is built differently with it. And the original example included it. |
Yep, you're right -- it passes 5 as a parameter for the |
Updating to {:ecto_sqlite3, "~> 0.18.1"} fixed the issue for me. :) |
So was this just due to outdated dependencies? |
In a sense, yes. But I think it can also be thought of as an issue with versioning due to |
Unsure. The only solution I could see offering is doing a patch release on the older version and bumping the min requirement to Edit: maybe setting an upper bound? |
Versions:
Relevant code
Issue
The above query function runs fine on Debian 12, but not on a Mac, as verified by @brownerd.
On Debian 12:
On Mac:
Any reason why
group_by/2
would not work on a Mac? It's the only thing that seems to be pertinently different between our respective setups--the other one being that I'm on Elixir 1.18.2, but the code was working fine even as far back as 1.14, so this can't be the root cause.Paging @brownerd in case he can provide more information about the platform.
The text was updated successfully, but these errors were encountered: