Skip to content

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

Open
@sparkybg

Description

@sparkybg

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);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions