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

Queries that include the count of a relation that has multiple foreign keys translates to incorrect SQL #7299

Closed
Tracked by #8628
frissonlabs opened this issue May 27, 2021 · 3 comments · Fixed by prisma/prisma-engines#2145
Assignees
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/client Issue for team Client. topic: postgresql topic: previewFeatures Issue touches on an preview feature flag topic: selectRelationCount
Milestone

Comments

@frissonlabs
Copy link

frissonlabs commented May 27, 2021

Bug description

Running a query to retrieve the count of a relation that has multiple foreign keys results in the following error:

Raw query failed. Code: `42804`. Message: `db error: ERROR: argument of JOIN/ON must be type boolean, not type record`

This appears to happen because the ON clause for the JOIN uses comma-separated boolean expressions instead of AND-seperated boolean expressions.

For example, this doesn't work:

SELECT * FROM "public"."User" LEFT JOIN (SELECT "public"."User".* FROM "public"."User") AS "user2" ON ("public"."User"."id" = "user2"."id","public"."User"."id" 
= "user2"."id")`

But this does:

SELECT * FROM "public"."User" LEFT JOIN (SELECT "public"."User".* FROM "public"."User") AS "user2" ON ("public"."User"."id" = "user2"."id" AND "public"."User"."id" 
= "user2"."id")`

I'd suggest that such queries use AND instead of commas during the transformation into raw SQL.

How to reproduce

Given the Prisma models included, running the following:

      const results = await db.userToObjective.findMany({
        include: {
          _count: {
            select: {
              votes: true,
            },
          },
        },
      })

Gives the following error:

Raw query failed. Code: `42804`. Message: `db error: ERROR: argument of JOIN/ON must be type boolean, not type record`

Expected behavior

It should successfully return an array of objects that include the vote count.

Prisma information

model User {
        id             	Int      @id @default(autoincrement())
	votes		Vote[]
}

model Objective {
	id                 Int       			@default(autoincrement()) @id
	name               String		 	@unique
}

model UserToObjective {
	user               User      			@relation(fields: [userId], references: [id])
	userId             Int
	objective          Objective 			@relation(name: "UserObjectives", fields: [objectiveId], references: [id])
	objectiveId        Int
	votes		   Vote[]			
	
	@@id([userId,objectiveId])
}

model Vote {
	createdAt          	DateTime  			@default(now())
	user               	User      			@relation(fields: [userId], references: [id])
	userId             	Int
	userObjective		UserToObjective			@relation(fields: [objectiveId, followerId], references: [objectiveId, userId])
	objectiveId		Int
	followerId              Int

	@@id([userId, objectiveId])
}

Environment & setup

  • OS: Windows 10, 64-bit
  • Database: PostgreSQL 12
  • Node.js version: 14.15.0

Prisma Version

2.23.0
@frissonlabs frissonlabs added the kind/bug A reported bug. label May 27, 2021
@janpio janpio added topic: selectRelationCount bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. topic: postgresql labels May 28, 2021
@frissonlabs

This comment has been minimized.

@janpio

This comment has been minimized.

@janpio janpio added the team/client Issue for team Client. label May 31, 2021
@matthewmueller matthewmueller added bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. and removed bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. labels Jul 6, 2021
@pantharshit00
Copy link
Contributor

Sorry for the late reply here, I can reproduce the crash. We need to fix the query generation.
Thanks for the detailed reproduction.

I tried it on 2.27.0-dev.33

@pantharshit00 pantharshit00 added bug/2-confirmed Bug has been reproduced and confirmed. and removed bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. labels Jul 6, 2021
@matthewmueller matthewmueller added process/candidate topic: previewFeatures Issue touches on an preview feature flag labels Jul 6, 2021
@matthewmueller matthewmueller added this to the 2.30.0 milestone Aug 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/client Issue for team Client. topic: postgresql topic: previewFeatures Issue touches on an preview feature flag topic: selectRelationCount
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants