Hi @nix0x00 👋

Thank you for raising this question.

Now that prisma has launched relations feature, how can I actually utilize it.

To use joins, you will need to enable the new relationLoadStrategy, you'll first need to add the preview feature flag to the generator block of your Prisma Client:

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["relationJoins"]
}

Once that's done, you'll need to re-run prisma generate for this change to take effect and pick a relation load strategy in your queries.
Here is an example that uses the new join strategy:

const usersWithPosts = await prisma.user.findMany({
  relationLoadStrategy: "join", // or "query"
  include: {
    posts: true,
  },
});

You should note that you can choose between database level join and application level join. With database level join, you would use join as the value for the relationLoadStrategy and with application level join, you would use query. You can take a look at this blog post to learn more.

can following query be converted to prisma query so that I don't have to use raw query.

If you want to convert this raw SQL query into a Prisma query, you might need to use the groupBy method provided by Prisma. But, as of now, Prisma does not support grouping on relations or counting based on a relation, as mentioned in the Github discussion. So at the moment, it's not currently possible to convert your raw SQL query into a Prisma query due to the limitations of the groupBy method.

If this answers your question, it would be great if you could mark this Discussion as answered to indicate that it has been resolved.

Otherwise please let us know how else we can help you further or close the Discussion if it was resolved in some other way 🙏

🌐
Prisma
prisma.io › home › relation queries › relation queries › relation queries › relation queries
Relation queries (Concepts) | Prisma Documentation
That means that it creates the JSON structures returned by Prisma Client already in the database which saves computation resources on the application level. You can use the relationLoadStrategy option on the top-level in any query that supports include or select.
🌐
Prisma
prisma.io › blog › prisma-orm-now-lets-you-choose-the-best-join-strategy-preview
Choosing the Best Join Strategy in Prisma ORM: join vs query
February 21, 2024 - With the query strategy, Prisma sends one query per table and merges the results in the application. You pick the strategy per query with the relationLoadStrategy option, available on PostgreSQL, CockroachDB, and MySQL behind the relationJoins preview feature flag; once the flag is on, join is the default.
Discussions

Specify per-relation load strategy
Note: this feature is related to the relationJoins preview feature launched in Prisma 5.8.0, and was discussed here. Problem It is often beneficial to use joins to load relations in a query, but it is sometimes better to load the related... More on github.com
🌐 github.com
0
January 22, 2024
Can I opt into join vs every query being join by default?
Hi @aliabbasrizvi To control the ... use the relationLoadStrategyrelationLoadStrategy option. This feature allows you to decide on a per-query basis how you want Prisma Client to execute a relation query. You can opt into join for specific queries by specifying the join strategy for individual queries as needed. For example... More on answeroverflow.com
🌐 answeroverflow.com
July 3, 2024
`relationJoins` default to `query`
When you enable the relationJoins ... use the relationLoadStrategy option in your queries to specify whether you want to use a join or the original query-based approach. This allows you to opt-in to testing joins for specific queries while maintaining the existing behavior for others. First, enable the relationJoins preview feature in your schema.prisma ... More on github.com
🌐 github.com
2
1
December 4, 2024
Support new relation load strategy for SQL Server (`relationJoins` preview feature)
Documentation: https://www.prisma.io/docs/orm/prisma-client/queries/relation-queries#relation-load-strategies-preview More on github.com
🌐 github.com
5
March 4, 2024
Top answer
1 of 1
1

Hi @nix0x00 👋

Thank you for raising this question.

Now that prisma has launched relations feature, how can I actually utilize it.

To use joins, you will need to enable the new relationLoadStrategy, you'll first need to add the preview feature flag to the generator block of your Prisma Client:

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["relationJoins"]
}

Once that's done, you'll need to re-run prisma generate for this change to take effect and pick a relation load strategy in your queries.
Here is an example that uses the new join strategy:

const usersWithPosts = await prisma.user.findMany({
  relationLoadStrategy: "join", // or "query"
  include: {
    posts: true,
  },
});

You should note that you can choose between database level join and application level join. With database level join, you would use join as the value for the relationLoadStrategy and with application level join, you would use query. You can take a look at this blog post to learn more.

can following query be converted to prisma query so that I don't have to use raw query.

If you want to convert this raw SQL query into a Prisma query, you might need to use the groupBy method provided by Prisma. But, as of now, Prisma does not support grouping on relations or counting based on a relation, as mentioned in the Github discussion. So at the moment, it's not currently possible to convert your raw SQL query into a Prisma query due to the limitations of the groupBy method.

If this answers your question, it would be great if you could mark this Discussion as answered to indicate that it has been resolved.

Otherwise please let us know how else we can help you further or close the Discussion if it was resolved in some other way 🙏

🌐
GitHub
github.com › prisma › prisma › issues › 22759
Specify per-relation load strategy · Issue #22759 · prisma/prisma
January 22, 2024 - // If unspecified, it inherits the global default. relationLoadStrategy: 'join', where: { // Only load posts for a specific author. authorId: '123', }, include: { // Load `post.category` via JOIN. category: { loadStrategy: 'join' }, // Load ...
Author   prisma
🌐
GitHub
github.com › prisma › prisma › discussions › 22288
Preview feature feedback: `relationJoins` · prisma/prisma · Discussion #22288
I don't know if you've seen it already, but we shipped a limited form of this in Prisma 5.8.0: you can specify whether to use JOINs or separate queries on a per Prisma query basis using the relationLoadStrategy argument, but it applies to all relations in the query.
Author   prisma
🌐
Nico's Blog
nico.fyi › blog › prisma-join-relation
The new join relation in Prisma | Nico's Blog
March 5, 2024 - const users = await prisma.user.findMany({ relationLoadStrategy: 'join', // or 'query'. The default is the new join strategy, so this can actually be omitted take: 10, include: { posts: { take: 3, }, }, }) In the example above, I aim to retrieve 10 users from the database along with 3 of their posts each...
🌐
Prisma
prisma.io › home › select fields › select fields › select fields › select fields › select fields
Select fields | Prisma Documentation
Since version 5.9.0, when doing a relation query with include or by using select on a relation field, you can also specify the relationLoadStrategy to decide whether you want to use a database-level join or perform multiple queries and merge the data on the application level. This feature is currently in Preview, you can learn more about it here. All following examples on this page are based on the following schema:
Find elsewhere
🌐
DeepWiki
deepwiki.com › prisma › docs › 5.3-relations-and-data-modeling
Relations and Data Modeling | prisma/docs | DeepWiki
November 11, 2025 - Since version 5.8.0, the relationLoadStrategy option controls how Prisma Client fetches related data: join (default): Uses LATERAL JOIN (PostgreSQL) or correlated subqueries (MySQL) with JSON aggregation · query: Sends separate queries per table and merges data at application level ... Within include or select, you can apply where, orderBy, take, and skip to filter related records. Example from content/200-orm/200-prisma-client/100-queries/037-relation-queries.mdx413-452:
🌐
Prisma
prisma.io › blog › prisma-6-better-performance-more-flexibility-and-type-safe-sql
Prisma 6: Better Performance, More Flexibility & Type-Safe SQL
November 28, 2024 - const usersWithPosts = await prisma.user.findMany({ relationLoadStrategy: "join", // or "query" include: { posts: true, }, }); If you want to learn more about how these two approaches work under the hood and when to prefer which JOIN strategy, check out this blog post: Prisma ORM Now Lets You Choose the Best Join Strategy (Preview). With Prisma ORM, you can create multiple new records in nested queries, for example:
🌐
Prisma
prisma.io › home › prisma client api › prisma client api › prisma client api
Prisma Client API | Prisma Documentation
const users = await prisma.user.findMany({ relationLoadStrategy: "join", select: { posts: true, }, }); where defines one or more filters, and can be used to filter on record properties (like a user's email address) or related record properties ...
🌐
Answer Overflow
answeroverflow.com › m › 1258099361745866752
Can I opt into join vs every query being join by default? - Prisma
July 3, 2024 - const result = await prisma.user.findMany({ include: { posts: true, }, relationLoadStrategy: 'join', // Opt into join for this specific query }); const result = await prisma.user.findMany({ include: { posts: true, }, relationLoadStrategy: 'join', // Opt into join for this specific query }); ...
🌐
Prisma
prisma.io › home › query optimization › query optimization › query optimization › query optimization
Query optimization | Prisma Documentation
You can perform the query with ... const users = await prisma.user.findMany({}); const userIds = users.map((x) => x.id); const posts = await prisma.post.findMany({ relationLoadStrategy: "join", where: { authorId: { in: userIds, }, }, });...
🌐
GitClear
gitclear.com › open_repos › prisma › prisma › release › 5.10.0
Prisma 5.10.0 Release - GitClear
await prisma.user.findMany({ relationLoadStrategy: 'join', // or 'query' include: { posts: true, }, }) Note that in the example above, the relationLoadStrategy could be omitted altogether because join is used as the default value. A few notes about relationLoadStrategy support on MySQL: ...
🌐
StudyRaid
app.studyraid.com › en › read › 11147 › 345644 › including-related-data
Including Related Data (Advanced Querying with Prisma Client)
For example, if you want to fetch a user along with their unpublished posts, sorted by title, you can use the following query: ... const userWithUnpublishedPosts = await prisma.user.findUnique({ where: { id: 1 }, include: { posts: { where: { ...
🌐
Safety DB
data.safetycli.com › packages › pypi › prisma › changelog
prisma Changelog
To get started, enable the Preview ... relation query via the `relationLoadStrategy` option as follows: ts await prisma.user.findMany({ relationLoadStrategy: 'join', // or 'query' include: { posts: true, }, }) Note that in the example above, the `relationLoadStrategy` could be ...
🌐
New Releases
newreleases.io › project › github › prisma › prisma › release › 5.10.0
prisma/prisma 5.10.0 on GitHub
February 20, 2024 - await prisma.user.findMany({ relationLoadStrategy: 'join', // or 'query' include: { posts: true, }, }) Note that in the example above, the relationLoadStrategy could be omitted altogether because join is used as the default value.
🌐
GitHub
github.com › prisma › prisma › discussions › 25789
`relationJoins` default to `query` · prisma/prisma · Discussion #25789
December 4, 2024 - For queries where you want to test the new join behavior, you can either omit the relationLoadStrategy option (as join is the default when the feature is enabled) or explicitly set it to 'join': const result = await prisma.user.findMany({ ...
Author   prisma
🌐
GitHub
github.com › prisma › prisma › issues › 23347
Support new relation load strategy for SQL Server (`relationJoins` preview feature) · Issue #23347 · prisma/prisma
March 4, 2024 - In versions 5.7.0 and 5.10.0 we released support for a new relation load strategy for PostgreSQL and MySQL as part of the relationJoins preview feature: Documentation: https://www.prisma.io/docs/orm/prisma-client/queries/relation-queries...
Author   prisma
🌐
X
x.com › prisma › status › 2013552133304725948
Prisma Postgres on X: "Did you know that Prisma ORM lets you control how relations are loaded? Use relationLoadStrategy: 'join' | 'query': - join runs a single DB query with joins - query runs multiple simpler queries Check it out👇 https://t.co/dy9Ryoze03 https://t.co/4J0lGqc4I8" / X
Did you know that Prisma ORM lets you control how relations are loaded? Use relationLoadStrategy: 'join' | 'query': - join runs a single DB query with joins - query runs multiple simpler queries Check it out https://pris.ly/d/relation-queries… · 2:00 AM ·
🌐
npm
npmjs.com › package › prisma › v › 5.8.0-integration-engines-5-8-0-36-feat-qe-implement-relationloadstrategy-api-d2f09986b7514e6bda7b9fa6b000aec0c4cd8180.1
prisma - npm
5.8.0-integration-engines-5-8-0-36-feat-qe-implement-relationloadstrategy-api-d2f09986b7514e6bda7b9fa6b000aec0c4cd8180.1 • Public • Published 2 years ago · Readme · Code Beta · 1 Dependency · 740 Dependents · 8,519 Versions · Quickstart • Website • Docs • Examples • Blog • Slack • Discord • Twitter · Prisma is a next-generation ORM that consists of these tools: Prisma Client: Auto-generated and type-safe query builder for Node.js & TypeScript ·