【问题标题】:How do I seed an mssql db using Prisma and createMany如何使用 Prisma 和 createMany 播种 mssql 数据库
【发布时间】:2021-08-21 15:24:19
【问题描述】:

我正在使用 Prisma - 客户端版本 2.23.0 - 与 mssql 数据库。我正在尝试使用 createMany 为数据库播种。我完全按照 Prisma 文档中的步骤进行操作。我已经成功运行了prisma generateprisma db push 命令。我的数据库和表已创建并且 Prisma 连接到它就好了。这是我正在使用的种子函数。

  // Prisma create query to seed models in database
  const count = await prisma.awDemographics.createMany({
    data: [
      {
        appointed_officials: 'some officials',
        awk_state: 'FL',
        meeting_schedule: 'MWF',
        pending_litigation: 'none',
        possible_competition: 'none',
        possible_contacts: 'none',
        voting_requirements: 'none',
      },
      {
        appointed_officials: 'some officials2',
        awk_state: 'FL2',
        meeting_schedule: 'MWF2',
        pending_litigation: 'none2',
        possible_competition: 'none2',
        possible_contacts: 'none2',
        voting_requirements: 'none2',
      },
    ],
  });
}

这是结果

Result:
PrismaClientUnknownRequestError2 [PrismaClientUnknownRequestError]: 
Invalid `prisma.awDemographics.createMany()` invocation:


  DEFAULT or NULL are not allowed as explicit identity values.

This is a non-recoverable error which probably happens when the Prisma Query Engine has a panic.

Error: Command failed with exit code 1: ts-node --eval "
// @ts-ignore
declare const require: any


console.info('Result:')

const __seed = require('./src/prisma/seed.ts')
const __keys = Object.keys(__seed)

async function runSeed() {
  // Execute "seed" named export or default export
  if (__keys && __keys.length) {
    if (__keys.indexOf('seed') !== -1) {
      return __seed.seed()
    } else if (__keys.indexOf('default') !== -1) {
      return __seed.default()
    }
  }
}

runSeed()
  .then(function (result) {
    if (result) {
      console.log(result)
    }
  })
  .catch(function (e) {
    console.error('Error from seed:')
    throw e
  })

我可以很好地使用create 函数播种。

这是我的架构:

  provider        = "prisma-client-js"
  previewFeatures = ["microsoftSqlServer"]
}

datasource db {
  provider = "sqlserver"
  url      = env("DATABASE_URL")
}

model AwDemographics {
  // @@map(name: "aw_demographics")
  id                             Int     @id @default(autoincrement())
  appointed_officials            String?
  awk_state                      String?
  meeting_schedule               String?
  pending_litigation             String?
  possible_competition           String?
  possible_contacts              String?
  voting_requirements            String?
}

【问题讨论】:

  • 你能分享你的schema.prisma吗?您也可以尝试升级到 2.24.0 看看是否可以解决问题?
  • 感谢您的快速响应。我已经添加了我的架构。将更新到 2.24.0 并再次测试。
  • 就是这样!去 2.24.0 工作。非常感谢,这让我发疯了。

标签: prisma seeding


【解决方案1】:

正如 Ryan 在 cmets 中所建议的,将 prisma 和 prisma 客户端更新到版本 2.24.0 解决了该问题。

【讨论】:

    猜你喜欢
    • 2023-02-16
    • 2021-08-31
    • 2023-02-21
    • 1970-01-01
    • 1970-01-01
    • 2018-12-14
    • 2018-04-13
    • 2019-12-12
    • 2015-06-29
    相关资源
    最近更新 更多