如何仅在Prisma中不是整个物体中的外键

发布于 2025-01-23 10:02:06 字数 938 浏览 0 评论 0原文

我刚刚开始学习Nestjs,我正在使用Prisma作为我的ORM,我创建了两个模型(员工,客户),这些模型将包含外国钥匙,客户将拥有员工ID,反之亦然(一个 - 一对一的关系),但是正如您将在模型中看到的那样,员工将包含整个客户端的“对象”,但是我想要的是他的客户ID作为独立列:

这是模型:

model Employe {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now())
  updated   DateTime @updatedAt

  email String @unique
  hash  String

  nom    String
  prenom String

 // I want the client ID here too
  
  client Client? 

  @@map("employes")
}

model Client {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now())
  updated   DateTime @updatedAt

  email String @unique
  hash  String

  nom    String
  prenom String

  EmpId Int?     @unique
  emp   Employe? @relation(fields: [EmpId], references: [id], onDelete: Cascade, onUpdate: Cascade)

  @@map("clients")
}

那么有什么方法?

  • 框架:Nestjs
  • Orm:Prisma
  • DB:PostgreSQL
  • 平台:Docker

I've just started learning nestjs and I'm using Prisma as my ORM, I've created two models (Employees, Clients), these models will contain foreign keys , the client will have the employee id and vice versa (a one-to-one relation), but as you will see in the models the employee will contain the whole Client "object", but what I want also is his client's id as an independent column :

this is the models:

model Employe {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now())
  updated   DateTime @updatedAt

  email String @unique
  hash  String

  nom    String
  prenom String

 // I want the client ID here too
  
  client Client? 

  @@map("employes")
}

model Client {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now())
  updated   DateTime @updatedAt

  email String @unique
  hash  String

  nom    String
  prenom String

  EmpId Int?     @unique
  emp   Employe? @relation(fields: [EmpId], references: [id], onDelete: Cascade, onUpdate: Cascade)

  @@map("clients")
}

So is there any way ?

  • Framework: NestJS
  • ORM: Prisma
  • DB : Postgresql
  • Platform: Docker

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

墨落成白 2025-01-30 10:02:06

客户端模型中的字段emp client在雇员模型中是关系字段,因此它们在数据库中不存在,它们只是在Prisma级别。

您只需要在关系的一侧定义外键 - 一对一的关系prisma参考

您可以将客户端存储在雇员上也是模型。

The fields emp in the Client model and client in Employe model are relation fields so they won't exist in the database, they are just at the prisma level.

You only need to define the foreign key on one side of the relation - One to One relation Prisma reference

You could store clientId on the Employe model as well.

凶凌 2025-01-30 10:02:06

我或多或少理解你的意思。我不明白为什么您不设置关系,但是在创建客户或雇员时可以使用此结构。

创建雇员

await prisma.employe.create({
  data: {
    email: 'blabla',
    hash: 'balbla',
    nom: 'blabla',
    prenom: 'balbla',
    clientId: 123,
    include: {
        client: {
           ...,
        }
    }
  },
})

创建客户

await prisma.client.create({
  data: {
    email: 'blabla',
    hash: 'balbla',
    nom: 'blabla',
    prenom: 'balbla',
    employeId: 123,
    include: {
        employe: {
           ...,
        }
    }
  },
})

I more or less understand what you mean. I don't understand why you don't set relation, but you can use this structure when creating a client or employe.

to create the employe

await prisma.employe.create({
  data: {
    email: 'blabla',
    hash: 'balbla',
    nom: 'blabla',
    prenom: 'balbla',
    clientId: 123,
    include: {
        client: {
           ...,
        }
    }
  },
})

to create the client

await prisma.client.create({
  data: {
    email: 'blabla',
    hash: 'balbla',
    nom: 'blabla',
    prenom: 'balbla',
    employeId: 123,
    include: {
        employe: {
           ...,
        }
    }
  },
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文