Skip to content
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

Count from subquery fails with missing alias in Postgres #1433

Open
eklatzer opened this issue Jan 11, 2025 · 0 comments
Open

Count from subquery fails with missing alias in Postgres #1433

eklatzer opened this issue Jan 11, 2025 · 0 comments

Comments

@eklatzer
Copy link

Hey, with v4.17.0 the following fix was introduced:

Count from subquery if query uses HAVING or GROUP BY. This is because aggregate functions are run for each group separately, but we need to return the count of returned rows.
PR: #1398

When using this fix with Postgres, the following error is returned: pq: subquery in FROM must have an alias

Here an example:

CREATE TABLE items (
    id serial PRIMARY KEY,
    category varchar(255) NOT NULL,
    is_special boolean NOT NULL
);

Example usage:

count, err := models.Items(
	models.ItemWhere.IsSpecial.EQ(true),
	// group because of possible joins to other tables
	qm.GroupBy(models.ItemColumns.ID),
).Count(boil.WithDebug(ctx, true), s.DB)
require.NoError(t, err)

SQL:

SELECT COUNT(*) FROM (SELECT * FROM "items" WHERE ("items"."is_special" = $1) GROUP BY id);
[true]

You can try it in https://onecompiler.com/postgresql/435bk9nra if you remove the as foo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant