Yaakov
10/19/2021, 6:45 PM"No 'State' record(s) (needed to inline the relation on 'Facility' record(s)) was found for a nested connect on one-to-many relation 'FacilityToState'."
.
How can I control the order in which my tables seed?
Here is my code:
seed.js
require('./seeds/stateSeeder');
require('./seeds/facilitySeeder');
stateSeeder.js
const faker = require('faker/locale/en_US');
const prisma = require('../client');
async function main() {
const states = faker.definitions.address.state;
const stateAbbrs = faker.definitions.address.state_abbr;
const data = states.map((state, i) => ({ name: state, abbr: stateAbbrs[i] }));
await prisma.state.createMany({ data });
}
main()
.catch((e) => {
console.error(e);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});
facilitySeeder.js
const prisma = require('../client');
async function main() {
await prisma.facility.create({
name: 'Name',
address: '123 Broad St',
city: 'Chicago',
zip: '60652',
state: {
connect: { abbr: 'IL' }
}
});
}
main()
.catch((e) => {
console.error(e);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});
client.js
const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient({ log: ['query'] });
module.exports = prisma
Chris Tsongas
10/19/2021, 6:59 PMseedStates
and seedFacilities
async functions, and moving main
into your seed.js
file so you can call main
just once then inside main
you can await one seed function before calling the other.Yaakov
10/19/2021, 7:06 PMYaakov
10/19/2021, 7:36 PM