Next.js Discord

Discord Forum

Adding "createdAt" and "updatedAt" into existing table with some data. Procedure, anyone?

Answered
Red wood ant posted this in #help-forum
Open in Discord
Red wood antOP
Title says it all. I'm trying to add the fields midstream. Prisma complains: "Step 0 Added the required column updatedAt to the business table without a default value. There are 23 rows in this table, it is not possible to execute this step. You can use prisma migrate dev --create-only to create the migration file, and manually modify it to address the underlying issue(s). Then run prisma migrate dev to apply it and verify it works." I tried the create-only route but I'm not sure the next steps to backfill the attributes for the existing records. Googling seems to lead to some sites that that don't really explain the full procedure. Anyone have a github or know of any resource that explains this common ( I'm assuming) situation?
Answered by joulev
since there are existing rows in the table, you need to configure what you want the createdAt and updatedAt fields for those rows to be

* if you want them to be null, just declare them as nullable to prisma with ?

* if you want them to be the current date, add @default(now())

* if you want to specify a different value then you'll need to do the two-step migration that prisma mentioned. it is unlikely this is what you want so i won't go into details for now.
View full answer

3 Replies

Answer
this is a good post to follow if you want to follow the third point (https://planetscale.com/blog/safely-making-database-schema-changes) but in your case i would just do the second point