Prisma在一对一的关系上插入外键的无效价值
相关模式零件。
model ShopifyOfflineStoreSession {
pk String @id(map: "PK_d371917bab72a48d6cce7880fc5") @default(dbgenerated("uuid_generate_v4()")) @db.Uuid
id String @unique(map: "UQ_91ede9e23efbd7ee27b37bdd9ba") @db.VarChar
store Store? @relation(fields: [pk], references: [shopifyOfflineSessionPk], onDelete: Cascade, onUpdate: Cascade, map: "FK_901a837fbbf20119aded3682f90")
@@map("shopify_store_session")
}
model Store {
id Int @id(map: "PK_f3172007d4de5ae8e7692759d79") @default(autoincrement())
shopifyOfflineSessionPk String? @unique(map: "UQ_901a837fbbf20119aded3682f90") @db.Uuid
shopifyOfflineSession ShopifyOfflineStoreSession?
@@map("store")
}
此查询失败:
await this.prisma.shopifyOfflineStoreSession.create({
data: {
...session,
store: { connect: { id: store.id } },
},
});
错误:
2022-06-10 16:41:02.035 UTC [15541] ERROR: null value in column "pk" violates not-null constraint
2022-06-10 16:41:02.035 UTC [15541] DETAIL: Failing row contains (null, offline_subbooks2.myshopify.com, 2022-06-10 16:41:02.033, 2022-06-10 16:41:02.033, null, subbooks2.myshopify.com, 005148992241858, f, null, null, null).
2022-06-10 16:41:02.035 UTC [15541] STATEMENT: INSERT INTO "public"."shopify_store_session" ("pk","id","createdAt","updatedAt","shop","state","isOnline") VALUES ($1,$2,$3,$4,$5,$6,$7) RETURNING "public"."shopify_store_session"."pk"
Issue saving Shopify offline token. Err: PrismaClientKnownRequestError:
Invalid `this.prisma.shopifyOfflineStoreSession.create()` invocation in
/usr/src/api/src/store/store.service.ts:576:52
573 data: session,
574 });
575 } else {
→ 576 await this.prisma.shopifyOfflineStoreSession.create(
Null constraint violation on the fields: (`pk`)
at cb (/usr/src/api/node_modules/@prisma/client/runtime/index.js:38683:17) {
code: 'P2011',
clientVersion: '3.6.0',
meta: { constraint: [ 'pk' ] }
}
query: DELETE FROM "store" WHERE "id" IN ($1) -- PARAMETERS: [18]
我不明白Prisma为何在shopifyflinestoressession上设置
pk
to null。有人可以帮助我了解导致此错误的原因吗?
Relevant schema parts.
model ShopifyOfflineStoreSession {
pk String @id(map: "PK_d371917bab72a48d6cce7880fc5") @default(dbgenerated("uuid_generate_v4()")) @db.Uuid
id String @unique(map: "UQ_91ede9e23efbd7ee27b37bdd9ba") @db.VarChar
store Store? @relation(fields: [pk], references: [shopifyOfflineSessionPk], onDelete: Cascade, onUpdate: Cascade, map: "FK_901a837fbbf20119aded3682f90")
@@map("shopify_store_session")
}
model Store {
id Int @id(map: "PK_f3172007d4de5ae8e7692759d79") @default(autoincrement())
shopifyOfflineSessionPk String? @unique(map: "UQ_901a837fbbf20119aded3682f90") @db.Uuid
shopifyOfflineSession ShopifyOfflineStoreSession?
@@map("store")
}
This query fails:
await this.prisma.shopifyOfflineStoreSession.create({
data: {
...session,
store: { connect: { id: store.id } },
},
});
Error:
2022-06-10 16:41:02.035 UTC [15541] ERROR: null value in column "pk" violates not-null constraint
2022-06-10 16:41:02.035 UTC [15541] DETAIL: Failing row contains (null, offline_subbooks2.myshopify.com, 2022-06-10 16:41:02.033, 2022-06-10 16:41:02.033, null, subbooks2.myshopify.com, 005148992241858, f, null, null, null).
2022-06-10 16:41:02.035 UTC [15541] STATEMENT: INSERT INTO "public"."shopify_store_session" ("pk","id","createdAt","updatedAt","shop","state","isOnline") VALUES ($1,$2,$3,$4,$5,$6,$7) RETURNING "public"."shopify_store_session"."pk"
Issue saving Shopify offline token. Err: PrismaClientKnownRequestError:
Invalid `this.prisma.shopifyOfflineStoreSession.create()` invocation in
/usr/src/api/src/store/store.service.ts:576:52
573 data: session,
574 });
575 } else {
→ 576 await this.prisma.shopifyOfflineStoreSession.create(
Null constraint violation on the fields: (`pk`)
at cb (/usr/src/api/node_modules/@prisma/client/runtime/index.js:38683:17) {
code: 'P2011',
clientVersion: '3.6.0',
meta: { constraint: [ 'pk' ] }
}
query: DELETE FROM "store" WHERE "id" IN ($1) -- PARAMETERS: [18]
I don’t understand why prisma is setting pk
on shopifyOfflineStoreSession
to null. Can someone help me understand what’s causing this error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
prisma docs 状态:
对我来说,我希望
store
记录能够在没有shopifyofflinestoresession
的情况下存在,这意味着我必须将关系标量
放在上ShopifyOfflinestoresession
而不是Store
。这就是最终状态现在的外观,也没有问题:
我必须执行一些迁移才能实现这一目标:
storeID
(nullable)(nullable)shopifyofflinestoresession
StoreID
non NullStore
shopifyofflineStoresessionshopify> shopify> shopifyflineseensepk )来自
存储
。如果您想要有关这些迁移的更多详细信息,请在评论中询问。
The Prisma docs state:
For me, I wanted a
Store
record to be able to exist without aShopifyOfflineStoreSession
which means I have to put therelation scalar
onShopifyOfflineStoreSession
instead ofStore
.This is how the final state looks now and there's no issues:
I had to execute a few migrations to achieve this:
storeId
(nullable) inShopifyOfflineStoreSession
Store
to itstoreId
non nullStore
toShopifyOfflineStoreSession
shopifyOfflineSessionPk
) fromStore
.If you want more details on these migration, ask in a comment.