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 · 3 comments · May be fixed by #1437
Open

Count from subquery fails with missing alias in Postgres #1433

eklatzer opened this issue Jan 11, 2025 · 3 comments · May be fixed by #1437

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

@renom
Copy link
Contributor

renom commented Jan 20, 2025

Can't reproduce on PostgreSQL 16.3.

What version do you use?

@renom
Copy link
Contributor

renom commented Jan 20, 2025

It seems versions prior to 16 require the alias.

@renom
Copy link
Contributor

renom commented Jan 20, 2025

Added PR with a fix.

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

Successfully merging a pull request may close this issue.

2 participants