1

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 Answers1

1

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.

Zoma
  • 321
  • 7
  • 20
  • I know about this, but the problem is that I myself am not writing the SQL commands. They are written automatically in the migration, and I want a way in Entity Framework to set the default value of all entries, both new and existing. I dont want to have to manually create SQL commands, I need the auto migrator to do that for me. – Sigfrid Stjärnholm Jun 24 '19 at 13:32
  • @SigfridStjärnholm I'm not very familiar with Entity, could you tell me where can you make changes that will generate the request ? – Zoma Jun 24 '19 at 13:36
  • @SigfridStjärnholm looks like you will not be able to to what you want without access to the migration. Have you already tried to create the column as a nullable then fill all existing columns with false ? – Zoma Jun 24 '19 at 13:55
  • Usually, you can sometimes define changes as attributes when defining the new property. Otherwise, you would change the Migration file to generate a different request. The first option doesnt exist in EF 6, the version Im using, and the second option is not feasable if you are using automatic migrations, as you are not able to edit this Migration file. – Sigfrid Stjärnholm Jun 24 '19 at 13:56
  • @SigfridStjärnholm given your limitations, can you try what I suggested in my previous comment ? – Zoma Jun 24 '19 at 14:04
  • I tried to do bool? which is a nullable bool, but it still gave me the same error. Im starting to feel like the easist way to do this right now is just to introduce some custom SQL code to create these columns and then set the default values using normal SQL. – Sigfrid Stjärnholm Jun 24 '19 at 14:13
  • 1
    Sorry, I tried to do bool? and the column was inserted correctly, and then i had to change all null values to false via a short SQL command. Seems like this is the best way to go. – Sigfrid Stjärnholm Jun 24 '19 at 14:37
  • @SigfridStjärnholm nice to hear that, I will edit my answer to add this solution. It may someone else. – Zoma Jun 24 '19 at 14:39