Skip to content

Commit 3317f9c

Browse files
committed
Don't generate both IDENTITY and NULL in migrations (#1849)
Fixes #1848
1 parent 7ca2944 commit 3317f9c

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

src/EFCore.PG/Migrations/NpgsqlMigrationsSqlGenerator.cs

+8-4
Original file line numberDiff line numberDiff line change
@@ -1419,12 +1419,16 @@ protected override void ColumnDefinition(
14191419
.Append(DelimitIdentifier(collation));
14201420
}
14211421

1422-
builder.Append(operation.IsNullable ? " NULL" : " NOT NULL");
1423-
1424-
DefaultValue(operation.DefaultValue, operation.DefaultValueSql, columnType, builder);
1425-
14261422
if (valueGenerationStrategy.IsIdentity())
1423+
{
14271424
IdentityDefinition(operation, builder);
1425+
}
1426+
else
1427+
{
1428+
builder.Append(operation.IsNullable ? " NULL" : " NOT NULL");
1429+
1430+
DefaultValue(operation.DefaultValue, operation.DefaultValueSql, columnType, builder);
1431+
}
14281432
}
14291433

14301434
// Note: this definition is only used for creating new identity columns, not for alterations.

test/EFCore.PG.FunctionalTests/Migrations/MigrationsNpgsqlTest.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ await Test(
150150

151151
AssertSql(
152152
@"CREATE TABLE ""People"" (
153-
""Id"" integer NOT NULL GENERATED BY DEFAULT AS IDENTITY
153+
""Id"" integer GENERATED BY DEFAULT AS IDENTITY
154154
);");
155155
}
156156

@@ -171,7 +171,7 @@ await Test(
171171

172172
AssertSql(
173173
@"CREATE TABLE ""People"" (
174-
""Id"" integer NOT NULL GENERATED ALWAYS AS IDENTITY
174+
""Id"" integer GENERATED ALWAYS AS IDENTITY
175175
);");
176176
}
177177

@@ -199,7 +199,7 @@ await Test(
199199

200200
AssertSql(
201201
@"CREATE TABLE ""People"" (
202-
""Id"" integer NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 10 INCREMENT BY 2 MAXVALUE 2000)
202+
""Id"" integer GENERATED ALWAYS AS IDENTITY (START WITH 10 INCREMENT BY 2 MAXVALUE 2000)
203203
);");
204204
}
205205

@@ -702,7 +702,7 @@ await Test(
702702
});
703703

704704
AssertSql(
705-
@"ALTER TABLE ""People"" ADD ""SomeColumn"" integer NULL GENERATED BY DEFAULT AS IDENTITY;");
705+
@"ALTER TABLE ""People"" ADD ""SomeColumn"" integer GENERATED BY DEFAULT AS IDENTITY;");
706706
}
707707

708708
[Fact]
@@ -727,7 +727,7 @@ await Test(
727727
});
728728

729729
AssertSql(
730-
@"ALTER TABLE ""People"" ADD ""SomeColumn"" integer NULL GENERATED ALWAYS AS IDENTITY;");
730+
@"ALTER TABLE ""People"" ADD ""SomeColumn"" integer GENERATED ALWAYS AS IDENTITY;");
731731
}
732732

733733
[Fact]
@@ -765,7 +765,7 @@ await Test(
765765
});
766766

767767
AssertSql(
768-
@"ALTER TABLE ""People"" ADD ""SomeColumn"" integer NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 5 INCREMENT BY 2 MINVALUE 3 MAXVALUE 2000 CYCLE CACHE 10);");
768+
@"ALTER TABLE ""People"" ADD ""SomeColumn"" integer GENERATED BY DEFAULT AS IDENTITY (START WITH 5 INCREMENT BY 2 MINVALUE 3 MAXVALUE 2000 CYCLE CACHE 10);");
769769
}
770770

771771
[Fact]
@@ -828,7 +828,7 @@ await Test(
828828
});
829829

830830
AssertSql(
831-
@"ALTER TABLE ""People"" ADD ""SomeColumn"" integer NOT NULL GENERATED BY DEFAULT AS IDENTITY;");
831+
@"ALTER TABLE ""People"" ADD ""SomeColumn"" integer GENERATED BY DEFAULT AS IDENTITY;");
832832
}
833833

834834
[Fact]

0 commit comments

Comments
 (0)