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

[SQL Server] add validation for disallowed relationships (e.g. cyclic) #4580

Closed
mavilein opened this issue Dec 10, 2020 · 1 comment · Fixed by prisma/prisma-engines#2113
Closed
Assignees
Labels
kind/improvement An improvement to existing feature and code. team/client Issue for team Client. topic: schema topic: sql server Microsoft SQL Server
Milestone

Comments

@mavilein
Copy link
Member

mavilein commented Dec 10, 2020

By default we render relation fields as a foreign key with an ON UPDATE CASCADE clause. This can cause problems with SQL Server as certain kinds of relation setups are not possible. Running migrations for those kinds of schemas results in errors such as:

Introducing FOREIGN KEY constraint FK__Artist__album_id__2D3D91F9 on table Artist may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

We must add validation to the parser for the following cases in order to show a nice error to the user.

Problem 1: Cyclic relationships

SQL Server does not allow to declare cyclic relationships such as the following.

model Album {
    id        Int      @id @default(autoincrement())
    artist_id Int
    artist    Artist   @relation(fields: [artist_id], references: [id])
    Artist    Artist[] @relation("FK_fav_album")
}

model Artist {
    id                Int     @id @default(autoincrement())
    favorite_album_id Int
    // this relation field makes it cyclic
    favorite_album    Album   @relation(fields: [favorite_album_id], references: [id], name: "FK_fav_album")
    albums            Album[]
}

Another example schema can be found here: #5782 (comment)

Problem 2: Multiple Cascade Paths

If you have more than two relations leaving from a model, they can't cross to the same model anywhere in their path.

model Album {
    id               Int      @id @default(autoincrement())
    FavoriteOf       Artist[] @relation("FK_fav_album")
    SecondFavoriteOf Artist[] @relation("FK_fav_album2")
}

model Artist {
    id                       Int   @id @default(autoincrement())
    favorite_album_id        Int
    second_favorite_album_id Int
    // this relation field makes it cyclic
    favorite_album           Album @relation(fields: [favorite_album_id], references: [id], name: "FK_fav_album")
    second_favorite_album    Album @relation(fields: [second_favorite_album_id], references: [id], name: "FK_fav_album2")
}

Problem 3: Self Relations

This is a special version of problem 1.

@janpio
Copy link
Member

janpio commented Jul 7, 2021

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. team/client Issue for team Client. topic: schema topic: sql server Microsoft SQL Server
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants