daily-code icon indicating copy to clipboard operation
daily-code copied to clipboard

Not able to do prisma db seed | help needed

Open killerkc12 opened this issue 10 months ago • 23 comments

The steps followed as below:

  1. Forked the repository.
  2. Clone the repository.
  3. Install the Dependencies
  4. Copied the env example
  5. Updated the .env file with the database URL
  6. Tried to migrate the Database with the following commands yarn prisma migrate dev - tables have been created successfully. yarn prisma db seed - This command is giving the below error

image

killerkc12 avatar Apr 13 '24 14:04 killerkc12

yarn prisma migrate dev yarn prisma db seed cd ../.. yarn run dev

kris08052000 avatar Apr 13 '24 14:04 kris08052000

Hi @kris08052000 , If you check the above description migrate command worked and the tables has created successfully. for the seed command it is giving the error screenshot attached in the description.

killerkc12 avatar Apr 13 '24 15:04 killerkc12

Hi @kris08052000 , If you check the above description migrate command worked and the tables has created successfully. for the seed command it is giving the error screenshot attached in the description.

Having the same issue @killerkc12. Have been trying to debug for last hour. Even reinstalled the whole project. Still the same error.

manavkush avatar Apr 13 '24 15:04 manavkush

@manavkush Same, even I tried to reinstall the whole project again.

killerkc12 avatar Apr 13 '24 15:04 killerkc12

Screenshot 2024-04-13 203633

Hi even i got the same issue. I changed to db from neon tech to docker local db and runned this command

  1. yarn prisma migrate dev
  2. yarn prisma db seed
  3. cd ../..
  4. yarn run dev

Now it is working

kris08052000 avatar Apr 13 '24 15:04 kris08052000

I think neon db is taking long time to db seed

kris08052000 avatar Apr 13 '24 15:04 kris08052000

External free databases are usually slow. Prefer using the docker postgres setup

SujithThirumalaisamy avatar Apr 13 '24 15:04 SujithThirumalaisamy

I've been running the command on my local docker postgres container. But the issue persists. Let me try using postgres instead of postgres:alpine. It shouldn't matter though.

manavkush avatar Apr 13 '24 15:04 manavkush

you can refer issue #196 Try to check if similar problems have already been discussed before creating issues to avoid having duplicate issues.

shubhamrawat090 avatar Apr 13 '24 15:04 shubhamrawat090

you can refer issue #196 Try to check if similar problems have already been discussed before creating issues to avoid having duplicate issues.

Your issue was failing due to a different issue. The error was not the same.

manavkush avatar Apr 13 '24 15:04 manavkush

Hey, @killerkc12 , I'm able to resolve the issue. I think the npx primsa migrate dev already ran the seed file also. Try to go into the container and check the Tracks table. I have the data inside it.

Might need to update the Readme file.

manavkush avatar Apr 13 '24 15:04 manavkush

you can refer issue #196 Try to check if similar problems have already been discussed before creating issues to avoid having duplicate issues.

Hi Shubham, Thank you for referring the issue. I've already seen the issue, and if you read the description of both issues, they are different and content also. where I was able to solve for first issue of migrating the db with #196 but later seeding the db was issue. I assume you understand this.

killerkc12 avatar Apr 13 '24 15:04 killerkc12

Hey, @killerkc12 , I'm able to resolve the issue. I think the npx primsa migrate dev already ran the seed file also. Try to go into the container and check the Tracks table. I have the data inside it.

Might need to update the Readme file.

Thank you @manavkush for informing me. Even I tried running the application and it worked.

killerkc12 avatar Apr 13 '24 15:04 killerkc12

you can refer issue #196 Try to check if similar problems have already been discussed before creating issues to avoid having duplicate issues.

Hi Shubham, Thank you for referring the issue. I've already seen the issue, and if you read the description of both issues, they are different and content also. where I was able to solve for first issue of migrating the db with #196 but later seeding the db was issue. I assume you understand this.

Yes, understood. I was just saying that because there this problem was being discussed in the comments.

shubhamrawat090 avatar Apr 13 '24 15:04 shubhamrawat090

you can refer issue #196 Try to check if similar problems have already been discussed before creating issues to avoid having duplicate issues.

Hi Shubham, Thank you for referring the issue. I've already seen the issue, and if you read the description of both issues, they are different and content also. where I was able to solve for first issue of migrating the db with #196 but later seeding the db was issue. I assume you understand this.

Yes, understood. I was just saying that because there this problem was being discussed in the comments.

Ok. Cool.

killerkc12 avatar Apr 13 '24 15:04 killerkc12

i know why this happens, basically when you run npx prisma db seed for the first time and some error occurs it still seeds some of the data that came before the error, then when you try to do npx prisma db seed again, some of the tables already will be filled so you cant insert it again since its not unique id. the solution to this is doing npx prisma sudio and checking which fields are populated and then deleting all of them, then try npx prisma db seed again

nischal-shetty2 avatar Apr 13 '24 17:04 nischal-shetty2

i know why this happens, basically when you run npx prisma db seed for the first time and some error occurs it still seeds some of the data that came before the error, then when you try to do npx prisma db seed again, some of the tables already will be filled so you cant insert it again since its not unique id. the solution to this is doing npx prisma sudio and checking which fields are populated and then deleting all of them, then try npx prisma db seed again

there was an error in the seed file till yesterday so whoever seeded yesterday got the error and since the fields before the error was already filled, when you ran the seed command it showed unique key error, why this works when you do it with docker today is because the seed issue was fixed today and you must have restarted the docker as well which obviously deletes all contents of the database and when you run docker again the npx prisma db seed ran perfectly the first time itself. why this didnt work with neon/ aiven DB string is because it didnt clear the half filled fields and when you run the command you get the error because there is already some content in the db

nischal-shetty2 avatar Apr 13 '24 17:04 nischal-shetty2

Hi, I was facing the same issue, and I resolved this by adding a new query parameter to the postgres connection URL in the .env file.

pool

where timeout is in seconds.

Note: Before seeding the db again, make sure to clear the database as some records would have been added during the previous seed and it will throw 'unique constraint error'.

yuvi3008 avatar Apr 13 '24 17:04 yuvi3008

Just change the package.json file inside the prisma folder.

  • Change prisma/seed.ts to prisma/seedsData.ts
  "prisma": {
    "seed": "ts-node prisma/seedsData.ts"
  },

Let me know if that helps

mvp5464 avatar Apr 13 '24 18:04 mvp5464

You can use npx prisma migrate reset, to reset the db. If any tables are populated, to get rid of the issue

nimit9 avatar Apr 13 '24 18:04 nimit9

Just change the package.json file inside the prisma folder.

  • Change prisma/seed.ts to prisma/seedsData.ts
  "prisma": {
    "seed": "ts-node prisma/seedsData.ts"
  },

Let me know if that helps

Don't do this!!! seedData.ts is the json data that is to be filled in the db and seed.ts is the actual logic that fills that json data to the db. They are written in separate files for clarity and readability purpose only.

shubhamrawat090 avatar Apr 14 '24 00:04 shubhamrawat090

Another way is just modifying the code a little bit in the seed.ts the async main function should first check whether there is existingTrack or not and if there is not existingTrack then only it should create the db.track.create i just change the async main function with these

async function main() {
  const hashID: any[] = [];
 const promises: Promise<any>[] = [];

 for (const seed of seedsData) {
    // Check if the id already exists in the database
    const existingTrack = await db.track.findUnique({
      where: { id: seed.data.id },
    });

    if (!existingTrack) {
      // If the id is unique, proceed with the insertion
      const promise = db.track.create({ data: seed.data });
      promises.push(promise);
      hashID.push(seed.data.id);
    } else {
      console.log(`Track with id ${seed.data.id} already exists.`);
      // Handle the situation as needed, e.g., skip insertion, generate a new id, etc.
    }
 }

 await Promise.all(promises);

Although there are some nextjs issues but this setups the projects!

Grgnavin avatar Apr 14 '24 07:04 Grgnavin

Or you can use db.track.upsert

nimit9 avatar Apr 14 '24 07:04 nimit9