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.

One-sided n:m relations

See original GitHub issue

In Prisma 1, it was possible to have something like

type Translation {
    id: ID! @id
    value: String!
    language: Language!
}
type Post {
    id: ID! @id @unique
    title: [Translation] @relation(name: "PostTitle")
    content: [Translation] @relation(name: "PostContent")
}

if I try something similar with the new prisma2 schema, I am getting

error: Error validating model “Post”: Named relations require an opposite field…

This is what I’ve tried:


model Translation {
    id String @default(cuid()) @id
    value String
    language String
}

model Post {
    id String @id @default(cuid())
    title Translation[] @relation(name: "PostTitle")
    content Translation[] @relation(name: "PostContent")
}

It does work when I add oppsite relationships:


model Translation {
    id String @default(cuid()) @id
    value String
    language String

    ignore1 Post[] @relation("PostTitle")
    ignore2 Post[] @relation("PostContent")
}

model Post {
    id String @id @default(cuid())
    title Translation[] @relation(name: "PostTitle")
    content Translation[] @relation(name: "PostContent")
}

But honestly, I’m not interested in those. Those will just clutter my data model and add methods I do not want to use to my client. Is there any way to avoid that? (Or will it be before prisma2 goes final?)

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:12
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
chrisuicommented, Aug 6, 2019

I’m encountering this with a createdBy and updatedBy pattern and it’s leading me down a very verbose road I’m not sure I really want to follow.

Consider following psuedo schema which is quite common…

model ModelA {
  createdBy   User? 
  updatedBy   User? 
}

model ModelB {
  createdBy   User? 
  updatedBy   User? 
}

model ModelC {
  createdBy   User? 
  updatedBy   User? 
}

model User {
  ...
}

I feel I should not have to be explicit about every relation to user in the user model. It should be allowed to simply be referenced in these cases.

Currently to resolve the errors we have to do something like the following

model ModelA {
  createdBy   User?   @relation(name: "UserCreatedModelA")
  updatedBy   User?   @relation(name: "UserUpdatedModelA")
}

model ModelB {
  createdBy   User?   @relation(name: "UserCreatedModelB")
  updatedBy   User?   @relation(name: "UserUpdatedModelB")
}

model ModelC {
  createdBy   User?   @relation(name: "UserCreatedModelC")
  updatedBy   User?   @relation(name: "UserUpdatedModelC")
}

model User {
  createdA   ModelA[]   @relation(name: "UserCreatedModelA")
  updatedA   ModelA[]   @relation(name: "UserUpdatedModelA")
  createdB   ModelB[]   @relation(name: "UserCreatedModelA")
  updatedB   ModelB[]   @relation(name: "UserUpdatedModelA")
  createdC   ModelC[]   @relation(name: "UserCreatedModelA")
  updatedC   ModelC[]   @relation(name: "UserUpdatedModelA")
}

Ambiguous relation detected and then Named relations require an opposite field errors get me here.

2reactions
DregondRahlcommented, Oct 28, 2020

I’m facing a similar issue, where almost every table has a createdby/updatedby column. Updating the users table with alias’s is becoming a bit of a challenge. Has there been any update on this possibility ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Tell If You're In a One-Sided Relationship
A one-sided relationship can be defined as an imbalanced interpersonal relationship where one person invests more energy or where one person ...
Read more >
Meaning of "n:m" and "1:n" in database design - Stack Overflow
m:n is used to denote a many-to-many relationship ( m objects on the other side related to n on the other) while 1:n...
Read more >
Tables Relations in SQL Server: One-to-One, One-to-Many ...
In One-to-One relationship, one record of the first table will be linked to zero or one record of another table. For example, each...
Read more >
Relationships - EF Core | Microsoft Learn
You can simply provide a foreign key on one side of the relationship. C# Copy. internal class ...
Read more >
Many-to-many relations - Prisma
How to define and work with many-to-many relations in Prisma. ... relations refer to relations where zero or more records on one side...
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