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

Implement a code action adding a unique constraint to a relation #1181

Closed
pimeys opened this issue Jun 23, 2022 · 0 comments · Fixed by #1188
Closed

Implement a code action adding a unique constraint to a relation #1181

pimeys opened this issue Jun 23, 2022 · 0 comments · Fixed by #1188
Assignees
Labels
kind/feature A request for a new feature. team/schema Issue for team Schema. topic: code action topic: relation
Milestone

Comments

@pimeys
Copy link
Contributor

pimeys commented Jun 23, 2022

Problem

Let's consider the following data model:

model Foo {
  id  Int   @id
  val Int
  Bar Bar[]
}

model Bar {
  id    Int @id
  fooId Int
  foo   Foo @relation(fields: [fooId], references: [val])
}

The relation gives a validation error:

error: Error parsing attribute "@relation": The argument `references` must refer to a unique criteria in the related model. Consider adding an `@unique` attribute to the field `val` in the model `Foo`.
  -->  schema.prisma:15
   | 
14 |   fooId Int
15 |   foo   Foo @relation(fields: [fooId], references: [val])
16 | }
   | 

It is hard to find in some cases the place where to add the needed constraint.

Suggested solution

Implement a code action to add the unique constraint to the correct position. This should also work with compound keys, adding a @@unique attribute to the correct model.

@pimeys pimeys added kind/improvement An improvement to existing feature and code. topic: code action topic: relation labels Jun 23, 2022
@janpio janpio added kind/feature A request for a new feature. process/candidate Candidate for next Milestone. and removed kind/improvement An improvement to existing feature and code. labels Jun 23, 2022
@floelhoeffel floelhoeffel added team/schema Issue for team Schema. and removed process/candidate Candidate for next Milestone. labels Jun 29, 2022
@Jolg42 Jolg42 added this to the 4.1.0 milestone Jun 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature A request for a new feature. team/schema Issue for team Schema. topic: code action topic: relation
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants