Any advice on where I should be looking
I think you said that you were a new developer just starting out, so want to share that one of the great lessons is that (most) people building software are out there trying to help you use it – so they try – for the most part – to guide you along the way with error messages. And one of the great skills starting out as a dev is learning how to read and recognize them.
Added the required column
userId to the
Post table without a default value. There are 2 rows in this table, it is not possible to execute this migration.
It’s saying that you tried to run a migration and it failed. The migration tried to add a
Post and that it is a required field (ie it needs a value and cannot be null) but you tried to add it with out a default value.
The issue is that in the Post table you have two existing records with data in it. When it tried to add the
userId it couldn’t because it needed a value – but there was no default.
Basically, you have two Posts with data in it – tried to connect a User, but it can’t connect it because it doesn’t know the userId to … well use.
The easiest way to fix this is:
Since this is a dev environment, just truncate the Post table or delete all the records. If there are 0 Posts, then it can add the column and won’t run into issues because no Posts need a userId.
Now, if you were in Production, you’d have to:
- Add the userId and let it be nullable.
- Then dataMigrate to assign posts to the users you need.
- Add a migration to make the userId required.
But, don’t do that. Just delete the dev Posts.
Now this assumes you do want every Post to have an author/user, which I imagine you do.
Note on default fields.
Sometimes you will add a column that is required, but you can give it a default value (like current datetime or 0 or FALSE).
Prisma does this via @default() as described here: https://www.prisma.io/docs/concepts/components/prisma-schema/data-model#defining-a-default-value