Prisma isn't doing joins.
Hi, I have just setup the relation strategy as we would like the ORM to use join queries.
But I have noticed it just won't do a join, even after setting the preview feature and then trying the
relationLoadStrategy.
Here is a minifed version:
And I am doing:
but I am still seeing this:
What is happening? why is it ignoring what I ask it?2 Replies
Skipping coffee for instant knowledge! Let's brew that AI answer now.
You're seeing the expected behavior for Prisma's
- PostgreSQL uses
relationLoadStrategy: 'join' on MySQL. When you enable the relationJoins preview feature and use relationLoadStrategy: 'join', Prisma does not use a traditional SQL JOIN clause for MySQL. Instead, it uses correlated subqueries to fetch related data in a single query. This is why your query output shows a subquery with JSON_OBJECT rather than a JOIN statement.
This is by design:- PostgreSQL uses
LATERAL JOIN and JSON aggregation.
- MySQL uses correlated subqueries with JSON aggregation to achieve similar results, not a JOIN keyword in the SQL output.
From the docs:
TheDocs: relationLoadStrategy (Preview) So, Prisma is not ignoring your instructions—it's just that for MySQL, the "join" strategy is implemented using correlated subqueries, not explicit SQLjoin(default) strategy uses a database-levelLATERAL JOIN(PostgreSQL) or correlated subqueries (MySQL) and fetches all data with a single query to the database.
JOIN clauses. The result is still a single query fetching all the data, which is the intended optimization.
If you need a traditional SQL JOIN for some reason, you would need to use a raw query. Otherwise, Prisma's approach is working as designed for MySQL with the preview feature enabled Docs: Nested reads.Was the bot response helpful?