`GROUP BY` with >1 column sharing the same name (ie: `id`) returns duplicates value for each column sharing that name.
peetss opened this issue · comments
Describe the bug
Demonstrate this issue by simply adding a GROUP BY
clause to the first query on the playground.
To Reproduce
-- create tables
CREATE TABLE "user" ("id" SERIAL NOT NULL, "name" text NOT NULL, CONSTRAINT "PK_cace4a159ff9f2512dd42373760" PRIMARY KEY ("id"));
CREATE TABLE "photo" ("id" SERIAL NOT NULL, "url" text NOT NULL, "userId" integer, CONSTRAINT "PK_723fa50bf70dcfd06fb5a44d4ff" PRIMARY KEY ("id"));
ALTER TABLE "photo" ADD CONSTRAINT "FK_4494006ff358f754d07df5ccc87" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION;
ALTER TABLE "user" ADD IF NOT EXISTS "name" text not null;
ALTER TABLE "user" ADD data jsonb;
-- insert data
INSERT INTO "photo"("url", "userId") VALUES ('photo-of-me-1.jpg', DEFAULT) RETURNING "id";
INSERT INTO "photo"("url", "userId") VALUES ('photo-of-me-2.jpg', DEFAULT) RETURNING "id";
INSERT INTO "user"("name", "data") VALUES ('me', '{"tags":["nice"]}') RETURNING "id";
UPDATE "photo" SET "userId" = 1 WHERE "id" = 1;
UPDATE "photo" SET "userId" = 1 WHERE "id" = 2;
INSERT INTO "photo"("url", "userId") VALUES ('photo-of-you-1.jpg', DEFAULT) RETURNING "id";
INSERT INTO "photo"("url", "userId") VALUES ('photo-of-you-2.jpg', DEFAULT) RETURNING "id";
INSERT INTO "user"("name") VALUES ('you') RETURNING "id";
UPDATE "photo" SET "userId" = 2 WHERE "id" = 3;
UPDATE "photo" SET "userId" = 2 WHERE "id" = 4;
-- ============== query data ===============
-- Joins supported, with a best effort to use indinces.
SELECT "user"."id" AS "user_id", "photo"."id" AS "photo_id"
FROM "user" "user"
LEFT JOIN "photo" "photo" ON "photo"."userId"="user"."id"
GROUP BY user_id;
pg-mem version
2.6.13
I'm seeing the exact same issue
Sadly I was unable to fix this and had to move to a real postgres database to move forward.