npgsql / efcore.pg

Entity Framework Core provider for PostgreSQL
PostgreSQL License
1.52k stars 223 forks source link

Migration does not specify a default value for non-nullable List<> properties #3244

Open hackf5 opened 4 weeks ago

hackf5 commented 4 weeks ago

Version: Npgsql.EntityFrameworkCore.PostgreSQL 8.0.4

Reproduction:

Add property public List<string> NewArray { get; set; } = []; to entity model.

Add Migration.

Actual:

migrationBuilder.AddColumn<List<string>>(
      name: "new_array",
      table: "some_table",
      type: "text[]",
      nullable: false);

Expected:

migrationBuilder.AddColumn<List<string>>(
      name: "new_array",
      table: "some_table",
      type: "text[]",
      defaultValue: new List<string>(),
      nullable: false);

Because the migration does not specify a default value, when the migration is run, when the table some_table is not empty the migration fails because it attempts to insert null into the non-nullable column.

roji commented 4 weeks ago

@hackf5 thanks for filing, but can you please show where this is actually affecting your program, e.g. causing an error?

hackf5 commented 4 weeks ago

Note added: migration fails due to attempt to insert null into non-nullable column.