Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not able to do prisma db seed | help needed #221

Open
killerkc12 opened this issue Apr 13, 2024 · 23 comments · May be fixed by #424
Open

Not able to do prisma db seed | help needed #221

killerkc12 opened this issue Apr 13, 2024 · 23 comments · May be fixed by #424

Comments

@killerkc12
Copy link

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

@kris08052000
Copy link

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

@killerkc12
Copy link
Author

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.

@manavkush
Copy link

manavkush commented Apr 13, 2024

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.

@killerkc12
Copy link
Author

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

@kris08052000
Copy link

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
Copy link

I think neon db is taking long time to db seed

@SujithThirumalaisamy
Copy link
Contributor

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

@manavkush
Copy link

manavkush commented Apr 13, 2024

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.

@shubhamrawat090
Copy link
Contributor

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

@manavkush
Copy link

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
Copy link

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.

@killerkc12
Copy link
Author

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
Copy link
Author

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.

@shubhamrawat090
Copy link
Contributor

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.

@killerkc12
Copy link
Author

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.

@nischal-shetty2
Copy link
Contributor

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
Copy link
Contributor

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

@yuvi3008
Copy link

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'.

@mvp5464
Copy link
Contributor

mvp5464 commented Apr 13, 2024

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

@nimit9
Copy link
Contributor

nimit9 commented Apr 13, 2024

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

@shubhamrawat090
Copy link
Contributor

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.

@Grgnavin
Copy link

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!

@nimit9
Copy link
Contributor

nimit9 commented Apr 14, 2024

Or you can use db.track.upsert

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants