Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding non-nullable field/column causes automatic migration issue. #106

Open
sparkybg opened this issue Jul 30, 2018 · 1 comment
Open

Comments

@sparkybg
Copy link

sparkybg commented Jul 30, 2018

Entity Framework 6.2.0
Npgsql 4.0.2
EntityFramework6.Npgsql 3.2.0

Adding integer field/column to table that currently have records in it causes error on automatic migrations:

'23502: column "xxxx" contains null values'

The executed SQL command is:

ALTER TABLE "xxx"."yyy" ADD "zzz" int4 NOT NULL

The same migration works in SQL Server and MySQL with no issues, setting the field in existing rows with type default value.

I think that a default value should be set for non-nullable columns when migrating, which must be the default value for type, in this case 0.

The SQL statement should be:

ALTER TABLE "xxx"."yyy" ADD "zzz" int4 NOT NULL DEFAULT 0

P.S: Annotating entity field with [DefaultValue(0)] does not change anything.

P.P.S:
The solution - in NpgsqlMigrationSqlGenerator, line 577, the following should be added:

else if (!(column.IsNullable ?? false) && column.ClrDefaultValue != null)
{
    sql.Append(" DEFAULT ");
    AppendValue(column.ClrDefaultValue, sql);
}
@sparkybg sparkybg changed the title Adding non-nullable field/column causes migration issue. Adding non-nullable field/column causes automatic migration issue. Jul 30, 2018
@sparkybg
Copy link
Author

sparkybg commented Sep 18, 2020

My last version of this:

            else if (column.IsNullable != null && !column.IsNullable.Value && column.ClrDefaultValue != null)
            {
                sql.Append(" DEFAULT ");
                AppendValue(column.ClrDefaultValue, sql);
            }

On the 6.4.1 release, this must be added between line 553 and line 554 of the NpgsqlMigrationSqlGenerator.cs file.

Is it so hard to add it in code? 2 years have passed. I am using my own local branch solely for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant