简体   繁体   中英

How do I seed an mssql db using Prisma and createMany

I am using Prisma - client version 2.23.0 - with an mssql database. I am trying to use createMany to seed the db. I followed the steps from the Prisma docs exactly. I have run the prisma generate and prisma db push commands successfully. My database and tables are created and Prisma connects to it just fine. Here is the seed function I am using.

  // 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',
      },
    ],
  });
}

Here is the result

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

I can seed using the create function just fine.

Here is my schema:

  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?
}

As suggested by Ryan in the comments, updating prisma and prisma client to version 2.24.0 fixed the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM