How would I add new boolean column with default value "false" without changing migration file?

.net entity-framework-6 postgresql psql

Question

I want to add a new property to an object in my EF model. The property is called "Deleted" and should have default value "false". All previous entries in the database need to be assigned this default value. I am using automatic migration, so changing the Up and Drop functions in the migration is not possible (that i know of, at least).

I have tried to just create the new property in the group class, but when the SQL code is ran (ALTER TABLE "dbo"."Group" ADD "Deleted" boolean NOT NULL) in psql, I get the error

ERROR: column "Deleted" contains null-values.

Note that I cannot intercept this call and insert constraints in the SQL command, it is called in the automatic migration. I have looked at this solution but as i am not using manual migration, I never have the chance to edit the Migration file. Any tips is greatly appreachiated! I have looked for many solutions, including using custom migration SQL generators, but none of it worked. The problem seems to be that all of the already existing entries in the database table get assigned null as the value for "Deleted".

1
1
6/24/2019 1:00:38 PM

Accepted Answer

The request you tried is missing the part with default value.

You should take a look at this PostgreSQL tutorial page.
Given information near the end of it, the following request would work for you :

ALTER TABLE "dbo"."Group" ADD "Deleted" boolean NOT NULL DEFAULT 'false'

If you can't access the Migration (as it is the case here), you may try to add the column as a nullable boolean column, then update the entire table with false value using SQL.

1
6/24/2019 2:42:19 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow