-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Optional relationship creates foreign key constraint #18058
Comments
Can you make |
Also faced the same problem. Setting the field (email in this case) to optional isn't a solution when the field itself should be mandatory but only the relation should be optional. This issue is similar to #8447 which is supposedly fixed by prisma/prisma-engines#2221 but the problem with that solution is that, from what I understood, it involves disabling native foreign keys completely, which is not always desirable. |
Anecdotally, I am experiencing this too. However, my DB is still in development. It appears that I was able to resolve this by nuking the DB's tables, and re-running This indicates to me that there's probably a bug in the migrate + generate pipeline that relies on DB introspection. |
@janpio that might solve the issue — but email is not optional, it's the relationship that's optional. I shouldn't have to allow for nullability on a column here as a workaround. |
How do you express an optional relationship in SQL without making the underlying field optional? |
Prisma 4.14 is creating a foreign key even when making both the field and relationship optional Context:
|
For Prisma currently "relation = foreign key". |
Any news on this? I can't imagine this being an edge case. Optional relations is quite common. UPDATE: sorry, at least in my case it was a wrong assumption, I had another issue. When you expect the relation to be optional, you should make sure to have the fields value really set to |
I'm running into this, are there any recommendations? I agree with @bcnichols3, in my case the scalar field is required but the relationship is optional |
@bcnichols3 The relation between these two models exists, or it does not exist. There is no in between. It's just that sometimes it is not used. In your case you can achieve that by moving the model User {
id String @id @default(cuid())
email String? @unique
author Author? @relation(fields: [email], references: [email])
}
model Author {
id String @id @default(cuid())
email String @unique
name String
user User?
} Then all (If that is not what you want to model, please describe it again so we can see how that can be express in a relational database) @dangnhdev What is the problem with the foreign key in this case? It is what is needed to guarantee referential integrity and actions for when the relation is actually used. @samualtnorman What is your use case - which situation do you want to model? |
hi @janpio i am currently facing this error too. model GeofenceMapping {
id Int @id @default(autoincrement())
uuid String @unique @default(uuid()) @db.Uuid
geofenceId String @map("geofence_id") @db.Uuid
entityType String @map("entity_type") @db.VarChar(64) // For example, 'user' or 'department' or 'company'
entityId String? @map("entity_id") @db.Uuid // The ID of the associated employee/department/company
geofence Geofence @relation(fields: [geofenceId], references: [uuid])
user User? @relation(fields: [entityId], references: [uuid], map: "user_mapping")
department Department? @relation(fields: [entityId], references: [uuid], map: "department_mapping")
company Company? @relation(fields: [entityId], references: [uuid], map: "company_mapping")
createdAt DateTime @default(now()) @map("created_at") @db.Timestamp()
updatedAt DateTime @default(now()) @updatedAt @map("updated_at") @db.Timestamp()
deletedAt DateTime? @map("deleted_at") @db.Timestamp()
@@unique([entityType, entityId, geofenceId])
@@index([uuid])
@@index([entityId])
@@index([entityType, entityId])
@@map("geofence_mapping")
} my query . //3. create geofence
const geofence = await this.geofenceRepository.createGeofence({
data: {
lat: createGeofenceDto.lat,
long: createGeofenceDto.long,
radius: createGeofenceDto.radius,
address: createGeofenceDto.address,
title: createGeofenceDto.title,
},
});
//4. map geofence to company
const mappedCompany = await this.geofenceRepository.createGeofenceMapping({
data: {
entityType: ENTITY.COMPANY,
geofence: {
connect: {
uuid: geofence.uuid,
},
},
company: {
connect: {
uuid: createGeofenceDto.companyId,
},
},
},
}); and i am getting this error.
Any idea why it is enforcing |
see #18058 (comment). |
@samualtnorman ooh thank you. I removed the relationships already and I'll do the inserts directly. Prisma relationships seem to be a bottleneck or hindrance sometimes. |
Our verdict is that Prisma is not designed to work with such databases. Our relationships are mapped to foreign keys, and foreign keys tell Prisma where a relation exists. We usually refer to relations without foreign keys as "weak relations", here is a feature request for them: #7351 |
any news on this one? |
Bug description
Optional relationships create a foreign key constraint, producing this error.
Note I had previously had this set as a non-optional relationship, but have since migrated away from that.
Seems to be similar to concerns posted here: prisma/prisma-engines#2221 posted by @rt2zz
How to reproduce
Expected behavior
Inserting an Author before a User record is created should not be rejected by the database.
Prisma information
Environment & setup
Prisma Version
The text was updated successfully, but these errors were encountered: