Reputation: 381
I want to add a new non-null column ("department" for example) to an existing entity ("employee" for example). The "employee" table already contains data, when run "$ nest start", I got the following error -
QueryFailedError: column "department" contains null values at QueryFailedError.TypeORMError [as constructor] (E:\repo\todo-js\src\error\TypeORMError.ts:7:9)
I tried to use typeORM migration to add a nullable "department" column and fill in data beforehand but turns out migration take place after typeORM data schema update when "$ nest start" is run. As such I got the above error even after I created the migration. Please note that I do not want to add a default value to the "department" column's definition as new employee insertion should specify the employee's department. How can I proceed with that? Thanks
(Part of the accepted answer is included in the comments below, you better read it all.)
Upvotes: 0
Views: 3586
Reputation: 2324
I see at least three ways:
department
column, fill it with existing data and then make it non-nullable;department
column with empty DEFAULT
value, fill it with data and then remove DEFAULT
;employee_new
table with department
column, fill it with data, remove old employee
table and rename employee_new
to employee
.Upvotes: 1