PacktPublishing / Full-Stack-Web-Development-with-Remix

Full Stack Web Development with Remix, published by Packt
MIT License
127 stars 24 forks source link

PrismaClientKnownRequest Error in chapter 8. #130

Closed princej02 closed 7 months ago

princej02 commented 7 months ago

I keep getting errors when running the seed for the databse. It had been a problem in past chapters but it managed to work but now i keep getting the message Unique constraints failed on fields: (email) and invalid db.user.create() on line 152. I did not change anything but keep getting an error. I've checked everywhere but i don't know why it fails.

This is what i get in terminal. code: 'P2002', clientVersion: '5.7.1', meta: { modelName: 'User', target: [ 'email' ] }

andrelandgraf commented 7 months ago

Hey @princej02,

The unique constraint error message tells you that there is already a user in the user table with the same email address. This probably means that you have already seeded the database. With the current setup, if you run npm run seed twice, it fails on the second run as the test user already exists.

You can run npm run reset:db to delete the existing database and create a new one, including running the seed script.

More context

In prisma/schema.prisma you can see that we have a unique constraint on the email property. This means that our database won't allow us to save two users with the same email value. This is important because we identify a user by their email address!

model User {
  id        String    @id @default(uuid())
  email     String    @unique
  name      String
  password  String
  createdAt DateTime  @default(now())
  updatedAt DateTime  @updatedAt
  expenses  Expense[]
  invoices  Invoice[]
}

You can learn more about unique in SQL here: https://www.w3schools.com/sql/sql_unique.asp

Please let me know if npm run reset:db works!

andrelandgraf commented 7 months ago

I just updated the instructions in the README files to document this for future readers: https://github.com/PacktPublishing/Full-Stack-Web-Development-with-Remix/commit/aec17e08736c851abd7b2b13701433fdffe796f2.

princej02 commented 7 months ago

Yeah it works now thanks.