Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limit implicit m-n relations to singular primary keys only #2262

Closed
albertoperdomo opened this issue Apr 21, 2020 · 1 comment
Closed

Limit implicit m-n relations to singular primary keys only #2262

albertoperdomo opened this issue Apr 21, 2020 · 1 comment
Assignees
Labels
kind/improvement An improvement to existing feature and code. tech/engines Issue for tech Engines.
Milestone

Comments

@albertoperdomo
Copy link
Contributor

albertoperdomo commented Apr 21, 2020

Problem

The implicit syntax for m-n relations today does not support all possible cases. Certain edge cases are not yet supported, making the Query Engine crash. To support these additional edge cases we would need to do additional engineering work and add more complexity to the m-n relation syntax.

Solution

We will reduce the complexity by dropping support for the composite keys edge cases.

Referencing tables without a single unique identifier will no longer be supported in the implicit syntax. Edge cases that will no longer supported with the implicit syntax:

Referenced model has @unique field, but no @id:

model Post {
  slug       Int        @unique // no @id
  categories Category[] @relation(references: [id])
}
model Category {
  id    Int    @id @default(autoincrement())
  posts Post[] @relation(references: [slug])
}

Referenced model has composite keys @@id:

model User {
  firstName     String
  lastName      String
  authoredPosts Post[] @relation(references: [id])

  @@id([firstName, lastName])
}

model Post {
  id      Int    @id
  authors User[] @relation(references: [firstName, lastName])
}

Referenced model has @@unique, but not @id:

model User {
  firstName     String
  lastName      String
  authoredPosts Post[] @relation(references: [id])

  @@unique([firstName, lastName])
}

model Post {
  id      Int    @id
  authors User[] @relation(references: [firstName, lastName])
}

Simplifying the Syntax

Given that only a subset of the cases is now supported, we can simplify the syntax, and the use of @relation to specify the foreign key field is no longer required.

Old syntax using @relation:

model Post {
  id         Int        @id @default(autoincrement())
  categories Category[] @relation(references: [id])
}
model Category {
  id    Int    @id @default(autoincrement())
  posts Post[] @relation(references: [id])
}

New syntax without @relation:

model Post {
  id         Int        @id @default(autoincrement())
  categories Category[]
}
model Category {
  id    Int    @id @default(autoincrement())
  posts Post[]
}

Diff:

model Post {
  id         Int        @id @default(autoincrement())
-  categories Category[] @relation(references: [id])
+  categories Category[]
}
model Category {
  id    Int    @id @default(autoincrement())
-  posts Post[] @relation(references: [id])
+  posts Post[]
}

@relation would remain as optional feature to disambiguate a relation (specs). Example:

model Blog {
    id         Int @id
    author     User[] @relation("Authorship")
    subscriber User[] @relation("Subscription")
}

model User  {
    id           Int @id
    authorOf     Blog[] @relation("Authorship")
    subscribedTo Blog[] @relation("Subscription")
}

In the case of n-m self-relations, the use of @relations("Name") for disambiguation will still be required. Example:

model User {
  friends  User[] @relation("Friends")
  friendsOf User[] @relation("Friends")
}

Scope

  • Limit implicit syntax to relations referencing single primary keys
  • Simplify the syntax by removing the need for @relation
  • Update documentation

Alternatives

  • Expand feature to support relations w/o primary or composite keys
  • Limit feature to support w/ primary or composite keys (make what we have today work without errors)
  • Remove the implicit syntax for m-n relations

Additional context

Internal: https://www.notion.so/prismaio/Support-implicit-syntax-for-m-n-relations-w-composite-keys-865a5ae609d34f86a4a62e2f2410d866

@albertoperdomo albertoperdomo added process/candidate kind/improvement An improvement to existing feature and code. labels Apr 21, 2020
@albertoperdomo albertoperdomo changed the title [Placeholder] Limit implicit relations for m-n relations to reference tables with single unique identifiers [Placeholder] Limit implicit relations for m-n relations to work only when references are pointing to tables to single unique identifiers Apr 21, 2020
@mavilein
Copy link
Member

related: prisma/prisma-engines#566

@albertoperdomo albertoperdomo changed the title [Placeholder] Limit implicit relations for m-n relations to work only when references are pointing to tables to single unique identifiers [Placeholder] Limit implicit m-n relations to models with single field id Apr 21, 2020
@janpio janpio added this to the Beta 4 milestone Apr 21, 2020
@janpio janpio added tech/engines Issue for tech Engines. and removed process/candidate labels Apr 21, 2020
@mavilein mavilein self-assigned this Apr 22, 2020
@albertoperdomo albertoperdomo changed the title [Placeholder] Limit implicit m-n relations to models with single field id Limit implicit m-n relations to primary keys only Apr 30, 2020
@divyenduz divyenduz modified the milestones: Beta 4, Beta 5 May 4, 2020
@janpio janpio modified the milestones: Beta 5, Beta 6 May 12, 2020
@janpio janpio modified the milestones: Beta 6, Beta 7, Beta 8 May 26, 2020
mavilein added a commit to prisma/prisma-engines that referenced this issue Jun 3, 2020
@janpio janpio modified the milestones: Beta 9, New Beta 9 Jun 8, 2020
@do4gr do4gr changed the title Limit implicit m-n relations to primary keys only Limit implicit m-n relations to singular primary keys only Jun 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/improvement An improvement to existing feature and code. tech/engines Issue for tech Engines.
Projects
None yet
Development

No branches or pull requests

4 participants