Skip to content

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

Closed
prisma/prisma-engines
#2145

Description

@frissonlabs

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

Activity

  1. added
    bug/0-unknown
    on May 28, 2021
  2. frissonlabs commented on May 29, 2021

    @frissonlabs
    Author
  3. janpio commented on May 29, 2021

    @janpio
  4. added
    bug/1-unconfirmed
    and removed
    bug/0-unknown
    on Jul 6, 2021
  5. pantharshit00 commented on Jul 6, 2021

    @pantharshit00
    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

  6. added
    bug/2-confirmed
    and removed
    bug/1-unconfirmed
    on Jul 6, 2021
  7. added
    topic: previewFeatures
    on Jul 6, 2021
  8. added this to the 2.30.0 milestone on Aug 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug/2-confirmedkind/bugtopic: postgresqltopic: previewFeaturestopic: selectRelationCount

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions