如何修复“创建者不存在……”在Prisma中?

发布于 2025-02-02 16:52:54 字数 1242 浏览 1 评论 0 原文

我计划为我的项目表创建一个播种机。我正在使用 createMany 将多个数据插入一个查询(请参见下面的代码)。但是问题是,它无法识别 createMany 并在进行开玩笑测试后抛出和错误。

另一件事使我感到困惑,我的代码中没有打字稿错误。我还可以使用创建函数创建单个数据。

我已经去过Prisma文档,但是我无法确定我的代码中出了什么问题。有人可以帮我弄清楚吗? (评论也将有所帮助)。

错误ts2339:属性'CreateMany'不存在于类型'ProviderDelegate< defferonnotfound |拒绝性ploperation |未定义的>'。


schema.prisma

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "sqlite"
  url      = "file:./dev.db"
}

model Provider {
  id Int @id @default(autoincrement())
  user_id Int
  name String
  space_key String
  api_key String
  projects Project[]
  created_at DateTime @default(now())
  updated_at DateTime @updatedAt
  @@unique([user_id, api_key])
}

我的用法

import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()

...

await prisma.provider.createMany({
  data: [
    {
      user_id: 1,
      name: 'Nicole Sal',
      space_key: 'nic_spa',
      api_key: 'nic_api',
      created_at: new Date(),
      updated_at: new Date()
    },
    // ... more data here (same at above)
  ]
})

I'm planning to create a seeder for my projects table. I'm using createMany to insert multiple data in just a query (see code below). But the problem is, it does not recognize createMany and throws and error after running a jest test.

Another thing that is confusing me, there was no typescript error in my code. And I can create also single data using create function.

I already been to prisma documentation, but I can't determine what was wrong in my code. Could someone help me figure it out. (comments would also help).

error TS2339: Property 'createMany' does not exist on type 'ProviderDelegate<RejectOnNotFound | RejectPerOperation | undefined>'.


schema.prisma

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "sqlite"
  url      = "file:./dev.db"
}

model Provider {
  id Int @id @default(autoincrement())
  user_id Int
  name String
  space_key String
  api_key String
  projects Project[]
  created_at DateTime @default(now())
  updated_at DateTime @updatedAt
  @@unique([user_id, api_key])
}

my usage

import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()

...

await prisma.provider.createMany({
  data: [
    {
      user_id: 1,
      name: 'Nicole Sal',
      space_key: 'nic_spa',
      api_key: 'nic_api',
      created_at: new Date(),
      updated_at: new Date()
    },
    // ... more data here (same at above)
  ]
})

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

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

发布评论

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

评论(2

も星光 2025-02-09 16:52:54

啊,我明白了。刚刚找到了这个。 createMany 不支持SQLITE。

不幸的是,在SQLite上不支持创造性:#10710
在此处记录: https://www.prisma。 io/doc/reference/api reference/prisma-client-reference#reverns-10

https://github.com/prisma/prisma/issues/11507#issuecomment-1025587202

Ahh I see. just found this. createMany is not supported for SQLite.

createMany is not supported on SQLite unfortunately: #10710
Documented here: https://www.prisma.io/docs/reference/api-reference/prisma-client-reference#remarks-10

https://github.com/prisma/prisma/issues/11507#issuecomment-1025587202

墨小沫ゞ 2025-02-09 16:52:54

更新:Prisma现在支持Sqlite上的创建人,请参见

但是,请注意:

您无法使用嵌套创建,无法创建或连接关系
造物主义者,连接,connectorCreate查询,顶部级别
CreateMany()Query。

如果这是为什么您看不到功能的原因,有个好消息:

您可以在更新()中使用嵌套的createMany查询或create()查询 - 例如,同时添加一个用户和两个帖子记录。

Update: Prisma now supports createMany on SQLite, see https://www.prisma.io/docs/orm/reference/prisma-client-reference#createmany

However, note that:

You cannot create or connect relations by using nested create,
createMany, connect, connectOrCreate queries inside a top-level
createMany() query.

If this is why you aren't seeing the function, there's good news:

You can use a nested a createMany query inside an update() or create() query - for example, add a User and two Post records with a nested createMany at the same time.

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