question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Documentation needed for modelling and querying complex Ternary relations.

See original GitHub issue

Problem

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:

Untitled Diagram

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:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

5reactions
pantharshit00commented, Mar 25, 2020

You can indeed implement such relation. An example implementation of the first schema would be:

model Permission {
  id String @id @default(cuid())
  name String
  usergroup UserGroups[]
}
model User {
  id String @id @default(cuid())
  usergroup UserGroups[]
}
model Group {
 id String @id @default(cuid())
 usergroup UserGroups[]
}
model UserGroups {
 user User
 group Group
 permission Permission
 @@id([user, group, permission])
}

(I removed _id prefixes from your example as that results in a more idiomatic generated API)

That will give you the following tables if you use migrate: image image image image

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

1reaction
MattGsoncommented, Apr 8, 2020

Example call for Find all the groups where user A has permissions B:

 const data = await prisma.group.findMany({
    where: {
      usergroup: {
        every: {
          user: {
            usergroup: {
              some: {
                permission: {
                  name: "B",
                },
              },
            },
          },
        },
      },
    },
  });

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Ternary Relationship - an overview | ScienceDirect Topics
Requirements Analysis and Conceptual Data Modeling ... We define a ternary relationship among three entities only when the concept cannot be represented by ......
Read more >
Entity-Relationship (ER) Models — CSCI 4380 Database ...
ER Data models design a whole database using entities and relationships. ... Simpler models are better: if ternary relationships are not needed, ...
Read more >
Ejb Ternary Relationship Example - C2 wiki
This small example is a practical demonstration of EjbFlaws in action. The assumption of the whole EJB effort is that it will offer...
Read more >
Guidelines for representing complex cardinality constraints in ...
Abstract Ternary relationships represent the association among three entities whose constraints database designers do not always know how to manage.
Read more >
Guidelines for representing complex ... - ResearchGate
The method shows how to calculate cardinality constraints in binary and ternary relationships and to preserve the associated semantics until the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found