登录  /  注册
Prisma 中的字段“id”外键约束失败
P粉023326773
P粉023326773 2023-12-26 11:38:58
[MySQL讨论组]

model Renting_Units {
  id              Int               @id @default(autoincrement())
  name            String            @unique(map: "name") @db.VarChar(255)
  description     String?           @db.VarChar(255)
  createdAt       DateTime          @default(now()) @db.DateTime(0)
  updatedAt       DateTime          @default(now()) @db.DateTime(0)
  User            Users             @relation(fields: [id], references: [id])
  created_by      Int
  Renting_Periods Renting_Periods[]
}


model Renting_Periods {
  id            Int           @id @default(autoincrement())
  product_id    Int           @db.Int
  Products      Products      @relation(fields: [product_id], references: [id], onUpdate: Cascade, onDelete: Cascade)
  start_date    DateTime      @db.DateTime(0)
  end_date      DateTime      @db.DateTime(0)
  createdAt     DateTime      @default(now()) @db.DateTime(0)
  updatedAt     DateTime      @default(now()) @db.DateTime(0)
  renting_unit  Int           @db.Int
  Renting_Units Renting_Units @relation(fields: [renting_unit], references: [id], onUpdate: Cascade, onDelete: Cascade)
  User          Users         @relation(fields: [id], references: [id])
  created_by    Int
  Invoices      Invoices[]
}

我收到外键 ID 错误。 我添加了一些租赁期,创建更多后出现此错误,但 ID 是自动递增的。 我尝试了网上的不同方法,但没有找到解决方案。 问题是它也发生过一次,但是我刷新了数据库,过了一段时间它又发生了。

我尝试插入新租赁期的代码:

await prisma.renting_Periods.create({
      data: {
        Products: {
          connect: {
            id: product_id,
          },
        },
        start_date,
        end_date,
        Renting_Units: {
          connect: {
            id: renting_unit_id,
          },
        },
        created_by: user_data.id,
      },
    });



The full erorr that displayed on the console:



→  74 await prisma.renting_Periods.create(
  Foreign key constraint failed on the field: `id`
    at RequestHandler.handleRequestError 
  clientVersion: '4.2.1',
  meta: { field_name: 'id' }

P粉023326773
P粉023326773

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 //m.sbmmt.com/ All Rights Reserved | php.cn | 湘ICP备2023035733号