Skip to content

SELECT aliases being referenced in WHERE statement clause Postgres #3669

@rstims

Description

@rstims

Describe the bug
Mikro seems to be using SELECT aliases in WHERE clause.

e.g.

SELECT "u0".*,
	"t1"."vendor_id" AS "technician_manager_vendor_id",
	"t1"."user_id" AS "technician_manager_user_id",
	"t2"."id" AS "technician_id"
FROM "user" AS "u0"
LEFT JOIN "technician_manager" AS "t1" ON "u0"."id" = "t1"."user_id"
LEFT JOIN "technician" AS "t2" ON "u0"."id" = "t2"."user_id"
WHERE NOT (("t1"."technician_manager_vendor_id","t1"."technician_manager_user_id") IS NULL)
ORDER BY "u0"."updated_at" DESC

Stack trace

InvalidFieldNameException: select "u0".*, "t1"."vendor_id" as "technician_manager_vendor_id", "t1"."user_id" as "technician_manager_user_id", "t2"."id" as "technician_id" from "user" as "u0" left join "technician_manager" as "t1" on "u0"."id" = "t1"."user_id" left join "technician" as "t2" on "u0"."id" = "t2"."user_id" where not (("t1"."technician_manager_vendor_id", "t1"."technician_manager_user_id") is null) order by "u0"."updated_at" desc limit 25 - column t1.technician_manager_vendor_id does not exist
        at PostgreSqlExceptionConverter.convertException (/Users/rstimmler/Code/mpx-api-service/node_modules/@mikro-orm/postgresql/PostgreSqlExceptionConverter.js:36:24)
        at PostgreSqlDriver.convertException (/Users/rstimmler/Code/mpx-api-service/node_modules/@mikro-orm/core/drivers/DatabaseDriver.js:192:54)
        at /Users/rstimmler/Code/mpx-api-service/node_modules/@mikro-orm/core/drivers/DatabaseDriver.js:196:24
        at processTicksAndRejections (node:internal/process/task_queues:95:5)
        at async PostgreSqlDriver.find (/Users/rstimmler/Code/mpx-api-service/node_modules/@mikro-orm/knex/AbstractSqlDriver.js:49:24)
        at async SqlEntityManager.find (/Users/rstimmler/Code/mpx-api-service/node_modules/@mikro-orm/core/EntityManager.js:106:25)
        at async UserResolver.usersConnection (/Users/rstimmler/Code/mpx-api-service/src/graphql/resolvers/user.ts:17:23)

To Reproduce
Steps to reproduce the behavior:

  1. Triggered when adding a where clause to check if a relationship is null
  2. Relationship has a composite primary key, both being an FK of another entity

User Entity

@Entity()
export class User extends BaseEntity<User> {
  @OneToOne(() => TechnicianManager, (technician_manager) => technician_manager.user)
  technician_manager: TechnicianManager
}

TechnicianManager Entity

@Entity()
export class TechnicianManager extends BaseEntityNoId<TechnicianManager, 'vendor' & 'user'> {
  @ManyToOne({ primary: true })
  vendor!: Vendor

  @OneToOne({ primary: true })
  user!: User
}

Where object

{
   "$and":[
      {
         "$or":[
            {
               "$not":{
                  "technician_manager":null
               }
            }
         ]
      }
   ]
}

Versions

Dependency Version
node 18.11.0
typescript 4.8.4
mikro-orm 5.5.0
your-driver postgres 14.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions