相关模式部分
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将shopifyOfflineStoreSession
上的pk
设置为null。有人能帮助我了解是什么导致这个错误吗?
Prisma文档状态:
在一对一关系中,关系中没有关系标量的那一面(在数据库中表示外键的字段)必须是可选的。
对于我来说,我希望Store
记录能够在没有ShopifyOfflineStoreSession
的情况下存在,这意味着我必须将relation scalar
放在ShopifyOfflineStoreSession
而不是Store
上。
这是最终状态现在的样子,没有问题:
model Store {
id Int @id(map: "PK_f3172007d4de5ae8e7692759d79") @default(autoincrement())
shopifyOfflineSession ShopifyOfflineStoreSession?
@@map("store")
}
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: [storeId], references: [id], onDelete: Cascade, onUpdate: Cascade, map: "FK_901a837fbbf20119aded3682f90")
storeId Int @unique
@@map("shopify_store_session")
}
我必须执行一些迁移来实现这一点:
- create
storeId
(null) inShopifyOfflineStoreSession
- 为
Store
添加适当的id - 使
storeId
非空 - 将
Store
更改为ShopifyOfflineStoreSession
- 从
Store
删除原FK (shopifyOfflineSessionPk
)
如果您想了解更多关于这些迁移的详细信息,请在评论中询问。