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.

Support recursive relationships

See original GitHub issue

Problem

Let’s say I have a table called categories. This table can refer to itself as a foreign key with parentCategory. So a categories can have many subcategories, and subcategories can also have subcategories, with an unlimited combination. The idea is to support recursive queries to easily query them along with their sub categories.

Suggested solution

Some valid implementation of something similar to Postgres WITH RECURSIVE https://www.postgresqltutorial.com/postgresql-recursive-query/

Alternatives

N/A

Additional context

N/A

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:117
  • Comments:29 (1 by maintainers)

github_iconTop GitHub Comments

13reactions
Southclawscommented, Feb 17, 2022

If the Prisma team is looking for a real-world example then I have one!

Here’s a schema with a Post model which has repies and also a root post. The reason it’s done this way is so there doesn’t need to be a separate model for “threads” and “posts” it’s all just a “post” and “first” indicates if it’s the first post in a thread.

And here’s a recursive CTE used to count the number of replies to threads (posts with “first” set to true)

If this could be expressed purely via Prisma, that would be awesome! I already hate the complexity and lack of composability with SQL syntax, but recursive CTEs are a special kind of hell!

10reactions
polypixeldevcommented, Nov 25, 2022

But isn’t this working already?

model Category {
  id                     Int      @id @default(autoincrement())
  parent_category_id     Int?
  [...more fields...]

  parentCategory         Category?  @relation("categoriesTocategories_parent_category_id", fields: [parent_category_id], references: [id])
  childCategory          Category[] @relation("categoriesTocategories_parent_category_id")

  @@map("categories")
}

I use this very often and never had problems.

I haven’t had an issue with recursive relationships in the schema, but querying them is the feature we’re looking for. How do you query your categories, since the depth is unknown?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Recursive Relationships in ER diagrams - GeeksforGeeks
A relationship between two entities of a similar entity type is called a recursive relationship. Here the same entity type participates more ...
Read more >
Recursive Relationships - erwin, Inc.
A recursive relationship is a non-identifying relationship between two entities or tables that represents the fact that one company can own another company....
Read more >
Recursive Relationship - an overview | ScienceDirect Topics
Recursive relationships represent self-referencing or involuting relationships. ... The original ER model supports N-ary relationships.
Read more >
7.4.4: Recursive Relationships - eCampusOntario Pressbooks
A relationship is recursive if the same entity type appears more than once. A typical business example is a rule such as “an...
Read more >
Chapter 6 One-to-One and Recursive Relationships
A recursive relationship is within a single entity rather than between entities. Recursive relationships are mapped to the relational model in the same...
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