Migrations with new notNull() columns
what's the proper way to add a notNull() column through a migration? I just tried to add a column in my schema like
and generated the migration with the standard
npx drizzle-kit generate:pg
command
but during the await migrate(...)
call, I get an error that existing rows have a NULL value (which, yes... i'm just adding the column now). I don't see anything in the docs about customizing migrations to backfill existing rows in cases like this. Is there a way to properly handle this?4 Replies
(I should add that if I add a
.default('bar')
it still has the same error... I presume that this default is only applied to newly inserted rows, so it doesn't help fix the existing rows...?)
this is the migration file:
and this is the error when I run the migrate(...)
code
Hello @ansipedantic! As your existing rows have a
NULL
value and you want to update your foo
column with notNull
option you can probably follow these steps:
1. Create a new column newFoo
with NOT NULL
and Default Value
: This allows you to add the column without directly affecting existing data.
2. Copy data from foo
to newFoo
: If there are existing data in the old column foo
that you want to retain, copy them into the new column.
3. Drop the old column foo
: After ensuring that all necessary data has been copied into the new column, you can safely remove the old column.
4. Rename newFoo
to foo
: The final step is to rename the new column back to the original column name.Hi @solo thanks for the reply. However, this is not the case I am trying to solve. I am adding a brand new column
foo
to an existing table.. I am not updating an existing column.
So, Step 1 in your above message is the equivilent of what I am trying to do, and what is causing the error
oh hmm hang on.. I thought I had an issue w/ using .notNull().default('asdf')
also throwing an error..
but i just tried again and it worked 😮
so, i need to make sure to provide a .default(...) when adding a .notNull() to an existing table
ok.. sorry for the confusion @solo - but your answer also helped me figure it out.. so thank you!Nice, happy that issue is solved!