Do you know why the foreign key constraint fail on...
# orm-help
d
Do you know why the foreign key constraint fail on the
postId
column? https://stackoverflow.com/questions/69776608/why-did-the-foreign-key-constraint-fail-on-the-column-postid I described the problem, but there is no answer.
r
@Dia TM 👋 Is the post with the
postId
available when you pass it like this?
Copy code
postId: faker.datatype.number({ min: 1, max: 7 }),
If not present, then it will throw an error. So make sure that all posts from id 1 to 7 are available.
d
Thank you. Thanks to you it works.
import { PrismaClient, Prisma } from '@prisma/client';
//import faker from 'faker';
const prisma = new PrismaClient();
const faker = require('faker');
const count = 14;
async function main() {
  `console.log(
Start seeding ...
);`  
for (let i = 0; i < count; i++) {
   
const user = await prisma.user.create({
     
data: {
       
email: faker.internet.email(),
       
firstName: faker.name.firstName(),
       
lastName: faker.name.lastName(),
       
avatar: faker.image.avatar(),
     
},
   
});
   
console.log({ user });
 
}
 
for (let i = 0; i < count; i++) {
   
const post = await prisma.post.create({
     
data: {
       
title: faker.name.title(),
       
content: faker.lorem.paragraphs(),
       
userId: faker.datatype.number({ min: 1, max: 7 }),
     
},
   
});
   
console.log({ post });
 
}
 
for (let i = 0; i < count * 2; i++) {
   
const image = await prisma.image.create({
     
data: {
       
imagePuth: faker.image.image(),
       
postId: faker.datatype.number({ min: 1, max: 7 }),
       
userId: faker.datatype.number({ min: 1, max: 7 }),
     
},
   
});
   
console.log({ image });
 
}
  `console.log(
Seeding finished.
);`
}
main()
 
.catch((e) => {
   
console.error(e);
   
process.exit(1);
 
})
 
.finally(async () => {
   
await prisma.$disconnect();
 
});
👍 1
💯 1