Prisma在一对一的关系上插入外键的无效价值

发布于 2025-02-06 22:33:08 字数 2288 浏览 2 评论 0原文

相关模式零件。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绝對不後悔。 2025-02-13 22:33:08

prisma docs 状态:

在一对一的关系中,关系的侧面没有关系标量(代表数据库中的外键的字段)必须是可选的。

对我来说,我希望store记录能够在没有shopifyofflinestoresession的情况下存在,这意味着我必须将关系标量放在上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")
}

我必须执行一些迁移才能实现这一目标:

  • 创建storeID(nullable)(nullable)shopifyofflinestoresession
  • 添加<<<代码>存储使其
  • 制作StoreID non Null
  • 更改FK从Store shopifyofflineStoresession
  • drop drobine frictional fk(shopify> shopify> shopifyflineseensepk )来自存储

如果您想要有关这些迁移的更多详细信息,请在评论中询问。

The Prisma docs state:

In a one-to-one relation, the side of the relation without a relation scalar (the field representing the foreign key in the database) must be optional.

For me, I wanted a Store record to be able to exist without a ShopifyOfflineStoreSession which means I have to put the relation scalar on ShopifyOfflineStoreSession instead of Store.

This is how the final state looks now and there's no issues:

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")
}

I had to execute a few migrations to achieve this:

  • create storeId (nullable) in ShopifyOfflineStoreSession
  • add appropriate ids from Store to it
  • make storeId non null
  • change FK from Store to ShopifyOfflineStoreSession
  • drop original FK (shopifyOfflineSessionPk) from Store.

If you want more details on these migration, ask in a comment.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文