Documentation needed for modelling and querying complex Ternary relations.
See original GitHub issueProblem
I can not find any documentation for working with ternary relations. As such, I’m not sure if and how they are supported. The documentation only seems to show very simple queries.
This is a basic task in SQL where many ORM’s fall flat which leads developers like myself to keep reaching for SQL.
From your own marketing: “For database access, developers can use traditional ORMs (e.g. Sequelize for Node.js or GORM for Go). While often beneficial to get a project off the ground, these aren’t a good longterm fit as project complexity quickly outgrows the capabilities of traditional ORMs.”
But there is no information about how Prisma can actually succeed where other ORM’s fail.
A common example is group permissions for users as demonstrated by the following table structures:

There are two common views that people would want for such data. I have given each view for the first and second schema respectively:
User view:
users =  [
{
    user_id: 2;
    groups: [
        {
            group_id: 4;
            permissions: {
                permission_id: 2;
            };
        },
    ];
}
]
users = [
{
    user_id: 2;
    groups: [
        {
            group_id: 4;
            permissions: [
                {
                    permission_id: 2;
                },
            ];
        },
    ];
}
]
Group view:
groups = [
{
    group_id: 2;
    users: [
        {
            user_id: 4;
            permissions: {
                permission_id: 2;
            };
        },
    ];
}
]
groups = [
{
    group_id: 2;
    users: [
        {
            user_id: 4;
            permissions: [
                {
                    permission_id: 2;
                },
            ];
        },
    ];
}
]
As well as viewing this data, it is also important that we can filter and sort by it as we could in SQL. For instance, one may which to answer the following query: ‘Find all the groups where user A has permissions B’.
Finally, these relationships should be able to be derived using introspection (assuming that they have foreign key relationships).
Solution
Prisma should clearly state in the documentation (with examples) how introspection and queries work with ternary and other complex relationships. Showing how Prisma can elegantly overcome this issue is important for proving that it worthy of use in production applications and different from “traditional ORMs”.
Issue Analytics
- State:
 - Created 3 years ago
 - Reactions:4
 - Comments:7 (4 by maintainers)
 

Top Related StackOverflow Question
You can indeed implement such relation. An example implementation of the first schema would be:
(I removed
_idprefixes from your example as that results in a more idiomatic generated API)That will give you the following tables if you use migrate:

We should have more docs on this though. Also, if you are unsure whether Prisma can represent your schema, you can always make your schema in SQL and introspect that schema using the cli:https://github.com/prisma/prisma2/blob/master/docs/introspection.md
Awesome! Thanks for the detailed explanation. It would be great to have something like this in the docs. It’s hard to be impressed by what is currently shown. This looks significantly more powerful than I expected.