You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
model User {
id String @default(cuid()) @id @unique
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
email String @unique
name String?
slack Provider? @relation(onDelete: CASCADE)
}
model Provider {
id String @default(cuid()) @id @unique
userId String @unique
accessToken String @unique
refreshToken String? @unique
user User? @relation(onDelete: CASCADE)
}
Reading the docs... "For 1:1 relationships, it doesn't matter on which side you store the foreign key. Prisma has a convention that the foreign key is added to the model which appears first alphanumerically in your data model".
But running SELECT * FROM sqlite_master; shows me differently
CREATE TABLE "Provider"("id" TEXT NOT NULL ,"userId" TEXT NOT NULL DEFAULT '' ,"accessToken" TEXT NOT NULL DEFAULT '' ,"refreshToken" TEXT ,PRIMARY KEY ("id"))
CREATE TABLE "User"("id" TEXT NOT NULL ,"createdAt" DATE NOT NULL ,"updatedAt" DATE NOT NULL DEFAULT '1970-01-01 00:00:00' ,"email" TEXT NOT NULL DEFAULT '' ,"name" TEXT ,"slack" TEXT NOT NULL REFERENCES Provider(id),PRIMARY KEY ("id"))
It also seems that @relation(onDelete: CASCADE) is not functioning as well. Deleting a provider should delete the fellow user, but the user referencing the deleted provider is not deleted
Right now we are creating the foreign key on the model which appears first in the project.prisma file. But it should be in accordance with lexicographical precedence.
After some testing, it seems the foreign key is determined by which referenced field on the two referencing models comes with lexicographical precedence.
Example 1 - userRef > userReff
Model
model User2 {
id String @default(cuid()) @id @unique
userRef User3
}
model User3 {
id String @default(cuid()) @id @unique
userReff User2
}
One more thing we want to keep in mind is creating a foreign key for an optional relation defined in the datamodel.
For example:
model User {
id String@default(cuid()) @id@unique
email String@unique
staff Staff?
}
model Staff {
id String@default(cuid()) @id@unique
user User
active Boolean?
}
If we create the database schema for the above datamodel using Lift, it will create a foreign key on the User field. But in the datamodel, Staff on a User is optional but a User is required on the Staff.
This shouldn't be the case. This is also easily fixable by creating the foreign key on staff rather than on the user.
model Staff {
id String@default(cuid()) @id@unique
user User@relation(references: [id])
active Boolean?
}
model User {
id String@default(cuid()) @id@unique
email String@unique
staff Staff?
}
Can you please open a new issue about this @tomitrescak? We will then look into it with the necessary information you provide about the problem. Thanks.
Update: I see you already did that in #810 - thanks!
Activity
HexSleeves commentedon Jul 3, 2019
It also seems that @relation(onDelete: CASCADE) is not functioning as well. Deleting a provider should delete the fellow user, but the user referencing the deleted provider is not deleted
pantharshit00 commentedon Jul 3, 2019
I can confirm this.
Right now we are creating the foreign key on the model which appears first in the project.prisma file. But it should be in accordance with lexicographical precedence.
Also, onDelete: CASCADE is yet to be implemented.
HexSleeves commentedon Jul 3, 2019
After some testing, it seems the foreign key is determined by which referenced field on the two referencing models comes with lexicographical precedence.
Example 1 - userRef > userReff
Model
Creates
Example 2 - userRef < userRe
Model
Creates
pantharshit00 commentedon Jul 9, 2019
Ref: graphql-nexus/nexus-prisma#328
One more thing we want to keep in mind is creating a foreign key for an optional relation defined in the datamodel.
For example:
If we create the database schema for the above datamodel using Lift, it will create a foreign key on the User field. But in the datamodel,
Staffon aUseris optional but aUseris required on theStaff.So when we try to create a User:
It will throw a Null Constraint violation exception as shown below:
This shouldn't be the case. This is also easily fixable by creating the foreign key on staff rather than on the user.
mavilein commentedon Jul 31, 2019
This is fixed. It will ship with
preview4.tomitrescak commentedon Oct 22, 2019
Hi, the CASCADE does not work though ;(
janpio commentedon Oct 22, 2019
Can you please open a new issue about this @tomitrescak? We will then look into it with the necessary information you provide about the problem. Thanks.
Update: I see you already did that in #810 - thanks!
Fixes #82
Fixes #82
Merge pull request #82 from prisma/tml-1790-guardrails-and-ci