Skip to content

Incorrect inverse join foreign key query when using balanced/select-in strategy to find tuples #6851

@adrien2p

Description

@adrien2p

Describe the bug

Hey, first of all thanks for the great work you are doing and the great ORM, we love it at @medusajs 💪
I d like to report an issue that I have previously mentionned along side this issue.

When using a filter in combination with populating a many to many relation, the inverse side of the address (one to one) is provoking the generation of an invalid query, causing the error: - column u2.address_id does not exist

Generated query:

SELECT
	"u0"."profile_id",
	"u0"."user_id",
	"p1"."id" AS "p1__id",
	"p1"."deleted_at" AS "p1__deleted_at",
	"p1"."name" AS "p1__name",
	"p1"."bio" AS "p1__bio"
FROM
	"user_profiles" AS "u0"
	INNER JOIN "profile" AS "p1" ON "u0"."profile_id" = "p1"."id"
	INNER JOIN "user" AS "u2" ON "u0"."user_id" = "u2"."id"
	LEFT JOIN "address" AS "a3" ON "u2"."id" = "a3"."user_id"
	AND "a3"."deleted_at" IS NULL
WHERE
	"u0"."user_id" IN (1)
	AND "p1"."deleted_at" IS NULL
	AND (
		"u2"."address_id" IS NULL
		OR "a3"."id" IS NOT NULL
	) - column u2.address_id does not exist

It seams that the foreign key is not applied to the correct side table in this scenario

Reproduction

https://github.com/adrien2p/mikroorm-6_5_4-reproduction-1/blob/main/src/example.test.ts
I made a typo during the repository creation using 6_5_4 instead of 6_5_3 sorry

What driver are you using?

@mikro-orm/postgresql

MikroORM version

6.5.3

Node.js version

v22.16.0

Operating system

MacOS Sequoia Version 15.6.1

Validations

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