Dev__
06/29/2021, 2:58 PMP2014 and now on 2.26 I get P2003 . Is this change related to the new update with referentialActions ?Mahmoud
06/29/2021, 3:00 PMreferentialActions are a feature flag. You have to enable them yourself in your schema.prisma fileMahmoud
06/29/2021, 3:01 PMDev__
06/29/2021, 3:03 PMDev__
06/29/2021, 3:03 PMP2014janpio
janpio
janpio
Dev__
06/29/2021, 3:07 PMDev__
06/29/2021, 3:08 PMBuyer_buyerGroupId_fkey (index)Dev__
06/29/2021, 3:08 PMjanpio
janpio
Mahmoud
06/29/2021, 3:15 PMDev__
06/29/2021, 3:17 PMmodel Buyer {
id
buyerGroupId String @db.VarChar(255)
buyerGroup BuyerGroup @relation(fields: [buyerGroupId], references: [id])
}
model BuyerGroup {
id
buyers Buyer[]
}
so before the new feature when I tried to delete a BuyerGroup record I would get an error with code P2014 because ofcourse there is a required relation Buyer record. something like this:
The change you are trying to make would violate the required relation '...' between the 'Buyer' and 'BuyerGroup' models.
After the update to 2.26 with the new feature I get:
Foreign key constraint failed on the field: 'Buyer_buyerGroupId_fkey (index)' with code P2003Mahmoud
06/29/2021, 3:27 PMDev__
06/29/2021, 5:47 PMMahmoud
06/30/2021, 2:26 PM2.22.0 when deleting a record that has a required relation, I got the following error:
Type: PrismaClientKnownRequestError
Message:
Invalid `prisma.buyerGroup.delete()` invocation:
The change you are trying to make would violate the required relation 'BuyerToBuyerGroup' between the `Buyer` and `BuyerGroup` models.
Code: P2014
Query:
prisma.buyerGroup.delete(
{
where: {
id: "5",
},
}
)
Upgrading to 2.26.0 gives the same error, which is expected.
Upgrading to 2.26.0 , enabling the referentialActions flag gives a different error message:
Type: PrismaClientKnownRequestError
Message:
Invalid `prisma.buyerGroup.delete()` invocation:
Null constraint violation on the fields: (`buyerGroupId`)
Code: P2011
Query:
prisma.buyerGroup.delete(
{
where: {
id: "5",
},
}
)
Prisma version:
prisma : 2.26.0
@prisma/client : 2.26.0
Current platform : darwin
Query Engine : query-engine 9b816b3aa13cc270074f172f30d6eda8a8ce867d (at node_modules/@prisma/engines/query-engine-darwin)
Migration Engine : migration-engine-cli 9b816b3aa13cc270074f172f30d6eda8a8ce867d (at node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine : introspection-core 9b816b3aa13cc270074f172f30d6eda8a8ce867d (at node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary : prisma-fmt 9b816b3aa13cc270074f172f30d6eda8a8ce867d (at node_modules/@prisma/engines/prisma-fmt-darwin)
Default Engines Hash : 9b816b3aa13cc270074f172f30d6eda8a8ce867d
Studio : 0.408.0
Preview Features : referentialActionsMahmoud
06/30/2021, 2:29 PMMahmoud
06/30/2021, 2:29 PMDev__
06/30/2021, 2:32 PMP2003. Would there be a difference with deleteMany and deleteDev__
06/30/2021, 2:33 PMDev__
06/30/2021, 2:35 PMMahmoud
06/30/2021, 2:36 PMdatasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
previewFeatures = ["referentialActions"]
}
model Buyer {
id String @id
buyerGroup BuyerGroup @relation(fields: [buyerGroupId], references: [id])
buyerGroupId String
}
model BuyerGroup {
id String @id
buyers Buyer[]
}janpio
janpio
ON DELETE CASCADE (I assumes - can you share the full SQL of your database @Dev__?) and that would violate the null constraint - and the error is thrown.janpio
Mahmoud
06/30/2021, 3:21 PMprisma and @prisma/client packages, added the feature flag and ran generatejanpio
janpio
Dev__
07/01/2021, 8:11 AMP2014 again. Using the new feature gives a different codeDev__
07/01/2021, 8:15 AMjanpio
New behavior. But instead of it deleting data, you are getting an error message.janpio
janpio
FOREIGN KEY ("buyerGroupId") REFERENCES "BuyerGroup" ("id") ON DELETE CASCADE ON UPDATE CASCADE.
Something is probably different in your project @Dev__ - can you maybe share the relevant bits of the SQL from your project?janpio
-- CreateTable
CREATE TABLE "Buyer" (
"id" TEXT NOT NULL PRIMARY KEY,
"buyerGroupId" TEXT NOT NULL,
FOREIGN KEY ("buyerGroupId") REFERENCES "BuyerGroup" ("id") ON DELETE CASCADE ON UPDATE CASCADE
);
-- CreateTable
CREATE TABLE "BuyerGroup" (
"id" TEXT NOT NULL PRIMARY KEY
);janpio
Dev__
07/01/2021, 1:03 PMjanpio
janpio
Dev__
07/01/2021, 1:04 PMjanpio
Dev__
07/01/2021, 1:06 PMDev__
07/01/2021, 1:14 PMcreate table "BuyerGroup"
(
id varchar(255) not null constraint "BuyerGroup_pkey" primary key,
"createdAt" timestamp with time zone default CURRENT_TIMESTAMP not null,
"updatedAt" timestamp with time zone not null
);
create table "Buyer"
(
id varchar(255) not null constraint "Buyer_pkey" primary key,
"buyerGroupId" varchar(255) not null constraint "Buyer_buyerGroupId_fkey" references "BuyerGroup" on update cascade on delete restrict,
"createdAt" timestamp with time zone default CURRENT_TIMESTAMP not null,
"updatedAt" timestamp with time zone not null
);
@janpiojanpio
Dev__
07/01/2021, 1:14 PMjanpio
janpio
Dev__
07/01/2021, 1:15 PMDev__
07/01/2021, 1:18 PMprisma push?janpio
FOREIGN KEY ("buyerGroupId") REFERENCES "BuyerGroup" ("id") ON DELETE CASCADE ON UPDATE CASCADE
Yours:
"buyerGroupId" varchar(255) not null constraint "Buyer_buyerGroupId_fkey" references "BuyerGroup" on update cascade on delete restrict,janpio
db push after upgrading and enabling the preview feature flag maybe?janpio
janpio
db push?Dev__
07/01/2021, 1:21 PMDev__
07/01/2021, 1:28 PMDev__
07/01/2021, 1:29 PMprisma db push after upgrading and enabling the new featurejanpio
janpio
PrismaClientUnknownRequestError2 [PrismaClientUnknownRequestError]:
Invalid `prisma.buyerGroup.delete()` invocation:
Error occurred during query execution:
ConnectorError(ConnectorError { user_facing_error: None, kind: QueryError(SqliteFailure(Error { code: ConstraintViolation, extended_code: 1811 }, Some("FOREIGN KEY constraint failed"))) })
at cb (C:\Users\Jan\Documents\throwaway\refActionsError\node_modules\@prisma\client\runtime\index.js:33900:17)
at processTicksAndRejections (internal/process/task_queues.js:93:5) {
clientVersion: '2.26.0'
}janpio
Dev__
07/01/2021, 1:31 PMMahmoud
07/01/2021, 1:31 PMjanpio
janpio
janpio
db push after the upgrade and adding the preview feature flag, you changed the behavior of the ON DELETE to the default, which is Restrict. And that is what causes the error message for you.janpio
PrismaClientKnownRequestError2 [PrismaClientKnownRequestError]:
Invalid `prisma.buyerGroup.delete()` invocation:
Foreign key constraint failed on the field: `Buyer_buyerGroupId_fkey (index)`
at cb (C:\Users\Jan\Documents\throwaway\refActionsError\node_modules\@prisma\client\runtime\index.js:33896:17)
at processTicksAndRejections (internal/process/task_queues.js:93:5) {
code: 'P2003',
clientVersion: '2.26.0',
meta: { field_name: 'Buyer_buyerGroupId_fkey (index)' }
}