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

Document SetNull validation introduced in [email protected] #4035

Open
jkomyno opened this issue Nov 9, 2022 · 0 comments
Open

Document SetNull validation introduced in [email protected] #4035

jkomyno opened this issue Nov 9, 2022 · 0 comments
Labels
docs Documentation creation, updates or corrections size: S more than one hour, up to one day topic: referential actions
Milestone

Comments

@jkomyno
Copy link
Contributor

jkomyno commented Nov 9, 2022

In the "Referential Actions" page, we should mention that, starting from [email protected], defining a Prisma schema with a SetNull referential action referencing a non-nullable field is no longer valid.

Consider the following Prisma models:

model SomeUser {
  id      Int      @id
  profile Profile?
}

model Profile {
  id     Int      @id
  user   SomeUser @relation(fields: [userId], references: [id], onUpdate: SetNull, onDelete: SetNull)
  userId Int      @unique
}

The @relation in Profile is not valid anymore, as the referenced field (user_id) has a non-nullable type (Int).

We have decided to add this new Prisma validation as, with the scenario above, all databases would fail with a validation error at the database level (which would result in a Prisma migration error). The only exception is Postgres, which accepts SET NULL referential actions, but would fail at runtime once such action is triggered by a change in a referenced row.

The new validation error message is:

Error parsing attribute "@relation": The onUpdate referential action of a relation must not be set to SetNull when a referenced field is required.
Either choose another referential action, or make the referenced fields optional.

Here's an example of a valid Prisma schema using SetNull appropriately:

model SomeUser {
  id      Int      @id
  profile Profile?
}

model Profile {
  id     Int       @id
  user   SomeUser? @relation(fields: [userId], references: [id], onUpdate: SetNull, onDelete: SetNull)
  userId Int?      @unique
}

Please check out the internal notion for additional context.

@jkomyno jkomyno added docs Documentation creation, updates or corrections topic: referential actions labels Nov 9, 2022
@jkomyno jkomyno added this to the 4.7.0 milestone Nov 9, 2022
@jharrell jharrell added the size: S more than one hour, up to one day label Nov 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation creation, updates or corrections size: S more than one hour, up to one day topic: referential actions
Projects
None yet
Development

No branches or pull requests

2 participants