Open
Description
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
- Query on a table
tableA
with.Group()
andtableA
has a foreign key towardstableB
- Wrap result in 1 as a table
newTable
- 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
Metadata
Metadata
Assignees
Labels
No labels