My seeds are running in the wrong order which is c...
# orm-help
y
My seeds are running in the wrong order which is causing this error:
"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
Copy code
require('./seeds/stateSeeder');
require('./seeds/facilitySeeder');
stateSeeder.js
Copy code
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
Copy code
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
Copy code
const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient({ log: ['query'] });

module.exports = prisma
c
The way you have this set up, you are calling async functions at the same time so there is no guarantee which will finish first. I'd recommend writing
seedStates
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.
y
@Chris Tsongas Thanks for your time! I'm a bit new to Node.js. Do you mind providing a code example of your intentions? Thank you!
Never mind. I got it! Thank you!!
🙌 1