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

Relation function generated wrong sql statement after WrapWith #1909

Open
woolen-sheep opened this issue Aug 16, 2021 · 1 comment
Open

Relation function generated wrong sql statement after WrapWith #1909

woolen-sheep opened this issue Aug 16, 2021 · 1 comment

Comments

@woolen-sheep
Copy link

Relation function generated wrong sql statement after WrapWith.
I wrote code like this:

	err := m.tx.Model((*Resume)(nil)).
		ColumnExpr("json_agg(tags) as tags, resume.*").
		Where("resume.period_id = ?", period).
		Join("JOIN tags ON resume.id = tags.resume_id").
		Group("resume.id").
		Order("create_time").
		WrapWith("resume").
		Table("resume").
		Relation("Turn").
		Where("turn.group_id = ?", group).
		Select(&resumes)

Expected Behavior

I want to get something like this:

WITH "resume" AS (SELECT json_agg(tags) as tags, resume.* FROM "resumes" AS "resume" JOIN tags ON resume.id = tags.resume_id WHERE (resume.period_id = 14) GROUP BY "resume"."id" ORDER BY "create_time") SELECT resume.*, "turn"."id" AS "turn__id", "turn"."name" AS "turn__name", "turn"."period_id" AS "turn__period_id", "turn"."group_id" AS "turn__group_id", "turn"."current" AS "turn__current", "turn"."status" AS "turn__status", "turn"."passed_message" AS "turn__passed_message", "turn"."not_passed_message" AS "turn__not_passed_message" FROM "resume" LEFT JOIN "turns" AS "turn" ON "turn"."id" = "resume"."turn_id" WHERE (turn.group_id = 17)

Current Behavior

go-pg generated sql statment below:

WITH "resume" AS (SELECT json_agg(tags) as tags, resume.*, "turn"."id" AS "turn__id", "turn"."name" AS "turn__name", "turn"."period_id" AS "turn__period_id", "turn"."group_id" AS "turn__group_id", "turn"."current" AS "turn__current", "turn"."status" AS "turn__status", "turn"."passed_message" AS "turn__passed_message", "turn"."not_passed_message" AS "turn__not_passed_message" FROM "resumes" AS "resume" LEFT JOIN "turns" AS "turn" ON "turn"."id" = "resume"."turn_id" JOIN tags ON resume.id = tags.resume_id WHERE (resume.period_id = 14) GROUP BY "resume"."id" ORDER BY "create_time") SELECT *, "turn"."id" AS "turn__id", "turn"."name" AS "turn__name", "turn"."period_id" AS "turn__period_id", "turn"."group_id" AS "turn__group_id", "turn"."current" AS "turn__current", "turn"."status" AS "turn__status", "turn"."passed_message" AS "turn__passed_message", "turn"."not_passed_message" AS "turn__not_passed_message" FROM "resume" LEFT JOIN "turns" AS "turn" ON "turn"."id" = "resume"."turn_id" WHERE (turn.group_id = 17)

And it cause ERROR #42803 column "turn.id" must appear in the GROUP BY clause or be used in an aggregate function because
turn.id has different values.

Possible Solution

Steps to Reproduce

  1. Query on a table tableA with .Group() and tableA has a foreign key towards tableB
  2. Wrap result in 1 as a table newTable
  3. Call .Relation("tableB") on the new table

Context (Environment)

Detailed Description

I want Relation function only select in the new table, DO NOT add those columns to the select statement in the wrapped table.

Possible Implementation

@elliotcourant
Copy link
Collaborator

It looks like if you add Column("_") to your query before the ColumnExpr("json_agg(tags) as tags, resume.*"). it should fix it.

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

2 participants