Repository navigation
Open
Labels
Description
Bug description
After upgrading to 2.27.0 I also started using the referential actions. In my e2e tests i delete all entities based on a tenantId (multi-tenant setup)
Now when using prisma.locations.deleteMany({where: {tenantId: "myTenantName"}}) I get:
Null constraint violation on the fields: (`tenantId`)
I checked the new migration file and the "Location" models foreign keys have not been touched (no change to the initial migration which is as expected).
The reason is with the SetNull prisma decides to set both ids to null which does not work for the model "Data" because tenantId is itself part of the composite primary key.
How to reproduce
Use provided model and this script:
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
async function main() {
const tenant = await prisma.tenant.create({
data: {
id: "myId"
}
})
await prisma.location.create({
data: {
id: "myId",
tenantId: tenant.id,
data: {
create: {
id: "myId",
}
}
},
})
await prisma.location.deleteMany({
where: {
tenantId: tenant.id
}
})
}
main()
.catch(e => {
throw e
})
.finally(async () => {
await prisma.$disconnect()
})
Expected behavior
Should set only the optional Id to null and ignore the tenantId since it's part of the composite primary key.
Prisma information
generator client {
provider = "prisma-client-js"
previewFeatures = ["referentialActions"]
}
datasource db {
provider = "postgresql"
url = "postgresql://admin:admin@localhost:5432/test?schema=public"
}
model Data {
tenantId String @db.VarChar
id String @db.VarChar
locationId String? @db.VarChar
location Location? @relation(fields: [tenantId, locationId], references: [tenantId, id], onDelete: SetNull)
@@id([tenantId, id])
}
model Location {
tenantId String @db.VarChar
id String @db.VarChar
data Data?
tenant Tenant @relation(fields: [tenantId], references: [id], onDelete: Cascade)
@@id([tenantId, id])
}
model Tenant {
id String @id @db.VarChar
locations Location[]
}
Environment & setup
- OS: Windows
- Database: PostgreSQL
- Node.js version: v16.4.0
Prisma Version
prisma : 2.27.0
@prisma/client : 2.27.0
Current platform : windows
Query Engine : query-engine cdba6ec525e0213cce26f8e4bb23cf556d1479bb (at node_modules\@prisma\engines\query-engine-windows.exe)
Migration Engine : migration-engine-cli cdba6ec525e0213cce26f8e4bb23cf556d1479bb (at node_modules\@prisma\engines\migration-engine-windows.exe)
Introspection Engine : introspection-core cdba6ec525e0213cce26f8e4bb23cf556d1479bb (at node_modules\@prisma\engines\introspection-engine-windows.exe)
Format Binary : prisma-fmt cdba6ec525e0213cce26f8e4bb23cf556d1479bb (at node_modules\@prisma\engines\prisma-fmt-windows.exe)
Default Engines Hash : cdba6ec525e0213cce26f8e4bb23cf556d1479bb
Studio : 0.410.0
Preview Features : referentialActions
Reacted by Dan Miller and Matt Alexander
Activity
janpio commentedon Jul 14, 2021
tlindener commentedon Jul 14, 2021
janpio commentedon Jul 16, 2021
I don't think Prisma is doing anything here. Referential actions live on a database level and are only configured in Prisma Schema. When you migrate your database, the generated SQL is executed and when you run a query later, that SQL just becomes active and does what it is configured to do.
In your example:
This is the logging you get when you have it all enabled (https://www.prisma.io/docs/concepts/components/prisma-client/working-with-prismaclient/logging + https://www.prisma.io/docs/concepts/components/prisma-client/debugging/):
As you can see it basically tries to run a
DELETE FROM "public"."Location" WHERE ("public"."Location"."tenantId","public"."Location"."id") IN (($1,$2))which then is declined by the database withError: Null constraint violation on the fields: (tenantId).janpio commentedon Jul 16, 2021
Here is the SQL of the database for context:
tlindener commentedon Jul 19, 2021
Yes I agree Prisma is not doing anything which isn't expected base on what I can set in the prisma schema.
Then again, I would hope to be able to do something like
location Location? @relation(fields: [tenantId, locationId], references: [tenantId, id], onDelete: [SetNull(id)]to tell prisma migrate to ignore the tenantId.
In other words, I would like the prisma schema to allow me being specific about the intent of the referential actions.
pantharshit00 commentedon Jul 20, 2021
I think this would be a feature that we will need to add to the schema in order to enable this.
cc @tomhoule
Workaround would be manually modify the migration which migrate allows.
thebiglabasky commentedon Aug 9, 2021
Ah yes, that's something that isn't supported at the database level though.
Having something like what is proposed here would introduce a mix-up: either it's
SetNulland gets transcribed into a database-level constraint (ON DELETE SET NULL) or it is something likeSetNul(id)and that has to be implemented at Prisma-level. This would introduce discrepancies that might cause trouble.We should ensure this is documented properly, and advise to use a trigger-like workaround for now.
EDIT: Docs already mention the limitation here: https://www.prisma.io/docs/concepts/components/prisma-schema/relations/referential-actions#setnull
kovaxis commentedon May 20, 2025
Note that this was implemented by PostgreSQL in version 15.0 (2022-10-13)
Changelog:
Documentation:
I +1 this issue, it is definitely a papercut.
I worked around it using custom PostgreSQL migrations: