Многое перепробовал не могу сгенерировать случайные id и уникальные от 1 до 2
Например
1 1
1 2
2 1
2 2
Git Sample
faker.random.number({ min: 1, max: 7 })
faker.unique(faker.datatype.number)
import { PrismaClient, Prisma } from '@prisma/client';
const prisma = new PrismaClient();
//import faker from 'faker';
const faker = require('faker');
async function main() {
console.log(`Start seeding ...`);
for (let i = 0; i < 7; i++) {
const user = await prisma.user.create({
data: {
email: faker.unique(faker.internet.email),
firstName: faker.name.firstName(),
lastName: faker.name.lastName(),
avatar: faker.image.avatar(),
posts: {
create: [
{
title: faker.name.title(),
content: faker.lorem.text(),
},
],
},
images: {
create: [
{
imagePuth: faker.image.image(),
postId: faker.datatype.number({ min: 1, max: 7 }), //error Foreign key constraint failed on the field: `postId`
postId: 1,3,2,5,6,7,4
},
],
},
},
});
console.log(`Created user with id: ${user.id}`);
}
console.log(`Seeding finished.`);
}
main()
.catch((e) => {
console.error(e);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});
Постоянно ошибка
import faker from 'faker'; //Ошибка
const faker = require('faker'); //Если так то автокомплит в ts не работает
"@types/faker": "^5.5.9",
"faker": "^5.5.3",
"devDependencies": {
"@nestjs/cli": "^8.0.0",
"@nestjs/schematics": "^8.0.0",
"@nestjs/testing": "^8.0.0",
"@types/express": "^4.17.13",
"@types/faker": "^5.5.9",
"@types/jest": "^27.0.1",
"@types/node": "^16.0.0",
"@types/supertest": "^2.0.11",
"@typescript-eslint/eslint-plugin": "^4.28.2",
"@typescript-eslint/parser": "^4.28.2",
"eslint": "^7.30.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.0",
"faker": "^5.5.3",
"jest": "^27.0.6",
"prettier": "^2.3.2",
"prisma": "^3.3.0",
"supertest": "^6.1.3",
"ts-jest": "^27.0.3",
"ts-loader": "^9.2.3",
"ts-node": "^10.0.0",
"tsconfig-paths": "^3.10.1",
"typescript": "^4.3.5"
},
An error occured while running the seed command:
Error: Command failed with exit code 1: ts-node prisma/seed.ts
PS Z:\spa\app> npx prisma db seed
Environment variables loaded from .env
Running seed command `ts-node prisma/seed.ts` ...
Start seeding ...
TypeError: Cannot read property 'unique' of undefined
at main (Z:\spa\app\prisma\seed.ts:121:22)
at Object.<anonymous> (Z:\spa\app\prisma\seed.ts:142:1)
at Module._compile (internal/modules/cjs/loader.js:1085:14)
at Module.m._compile (Z:\spa\app\node_modules\ts-node\src\index.ts:1371:23)
at Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
at Object.require.extensions.<computed> [as .ts] (Z:\spa\app\node_modules\ts-node\src\index.ts:1374:12)
at Module.load (internal/modules/cjs/loader.js:950:32)
at Function.Module._load (internal/modules/cjs/loader.js:790:12)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
at main (Z:\spa\app\node_modules\ts-node\src\bin.ts:331:12)
An error occured while running the seed command:
Error: Command failed with exit code 1: ts-node prisma/seed.ts