Gabe O'Leary
10/19/2021, 11:54 PMschema.prisma file looks like:
model User {
idStr String @id
name String?
statuses Status[]
}
model Status {
idStr String @id
createdAt DateTime
userIdStr String
user User? @relation(fields: [userIdStr], references: [idStr])
}
But I get this error:
Foreign key constraint failed on the field: `Status_userIdStr_fkey (index)`
when I try to create a status that has a userIdStr for which there is no corresponding User.
How do I set up my models such that there doesn't have to be a User present for a given Status.userIdStr?Hyo
10/20/2021, 2:41 AMuserIdStr to be optionalMo El
10/20/2021, 5:30 AMRyan
10/20/2021, 6:09 AMuserIdStr to be optional as well:
model Status {
idStr String @id
createdAt DateTime
userIdStr? String
user User? @relation(fields: [userIdStr], references: [idStr])
}Gabe O'Leary
10/21/2021, 7:48 PMGabe O'Leary
10/21/2021, 7:49 PMuserIdStr it's just that for some of these statuses there is no corresponding User record. Is it not possible to set up my schema to allow this type of relation?Ryan
10/22/2021, 4:48 AMuserIdStr , then every Status will also have a User. It’s not an either/or, the foreign key userIdStr is always mapped to the virtual field User. So if you know that userIdStr will always be present, remove the ? from both.