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.

Many-to-one or One-way relationships

See original GitHub issue

Problem

I find it problematic that relations can only be modelled as one->many instead of many->one.

An example use case:

model Post {
  id      String   @id
  User    User[]   @relation(fields: [userIds], references: [id])
  userIds String[]
}

model User {
  id           String   @id
  likes        Post[]   @relation(references: [id], fields: [likedPostIds])
  likedPostIds String[]
}

This simple data model would allow me to use prisma’s nice Fluent API, and do things like User.likes(). However, this is not scalable at all, if a Post has a million likes, the userIds array is going to be huge.

Suggested solution

Ideally, I would like to have the flexibility to decide for myself how the data is going to be structured. For my use case, I will never need to follow the relation from Post=>User, only User=>Post.

I wish the formatter would allow one-way relations, that way it could be as simple as:

model Post {
  id String @id
}

model User {
  id           String   @id
  likes        Post[]   @relation(references: [id], fields: [likedPostIds])
  likedPostIds String[]
}

Alternatives

The only alternative is to have a very granular “join table”, such as this:

model Post {
  id String @id @map("_id")

  LikesByUser LikesByUser[]
}

model LikesByUser {
  userId String
  post   Post   @relation(fields: [postId], references: [id])
  postId String
  User   User   @relation(fields: [userId], references: [id])

  @@id([userId, postId])
}

model User {
  id          String        @id @map("_id")
  LikesByUser LikesByUser[]
}

Other than inconvenience, the downside of doing this is that it isn’t very storage efficient.

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:7
  • Comments:16 (6 by maintainers)

github_iconTop GitHub Comments

7reactions
jonschlinkertcommented, Mar 29, 2022
  1. We do not allow one way relations. If you add a relation in one direction, we automatically also add the other direction.

We’re currently looking for a way to add one-way relations as well. We call these “references”. I’m not sure if this belongs on this issue, but consider a scenario where you might want to add “related items” to a post or product where it’s not only unnecessary, but undesirable to create a corresponding “link” on the other side of the relation. This is a very common pattern, and ideally we would be able to support this in a first class way with prisma. Instead, we have a number of different one-to-many, or many-to-many relations where one side of the relation will never be used.

An alternative would be to use array fields, or json, etc. But that defeats the purpose of using prisma for the fluent API, referential integrity, etc.

3reactions
harshitanejacommented, Jul 1, 2022

Any update on this? One way relationships are integral for our schema design. Is there any way to achieve that currently in prisma till official support lands?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Hibernate @ManyToOne - only one direction relation
Any way how to map relation @ManyToOne (many users can have the same adres), BUT I don't want to have property List< User...
Read more >
Hibernate @ManyToOne Unidirectional Tutorial
This is the meaning of a unidirectional relationship. We can only “look” at the relationship in one way. Now, if we wanted to...
Read more >
A Guide to JPA with Hibernate (Relationship Mappings)
We'd have two one-way relationships. ... By contrast, Many-to-One relationships are eager by default, meaning the relationship is loaded at ...
Read more >
The best way to map a @OneToMany relationship with JPA ...
This is the most natural way of mapping a database one-to-many database association, and, usually, the most efficient alternative too.
Read more >
Best Practices for Many-To-One and One ... - Thorben Janssen
Bidirectional one-to-many and both many-to-one association mappings are fine. But you should avoid unidirectional one-to-many associations in your domain model.
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