Skip to content

Commit

Permalink
Don't generate both IDENTITY and NULL in migrations (#1849)
Browse files Browse the repository at this point in the history
Fixes #1848
  • Loading branch information
roji committed May 27, 2021
1 parent 7ca2944 commit 3317f9c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
12 changes: 8 additions & 4 deletions src/EFCore.PG/Migrations/NpgsqlMigrationsSqlGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1419,12 +1419,16 @@ protected override void ColumnDefinition(
.Append(DelimitIdentifier(collation));
}

builder.Append(operation.IsNullable ? " NULL" : " NOT NULL");

DefaultValue(operation.DefaultValue, operation.DefaultValueSql, columnType, builder);

if (valueGenerationStrategy.IsIdentity())
{
IdentityDefinition(operation, builder);
}
else
{
builder.Append(operation.IsNullable ? " NULL" : " NOT NULL");

DefaultValue(operation.DefaultValue, operation.DefaultValueSql, columnType, builder);
}
}

// Note: this definition is only used for creating new identity columns, not for alterations.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ await Test(

AssertSql(
@"CREATE TABLE ""People"" (
""Id"" integer NOT NULL GENERATED BY DEFAULT AS IDENTITY
""Id"" integer GENERATED BY DEFAULT AS IDENTITY
);");
}

Expand All @@ -171,7 +171,7 @@ await Test(

AssertSql(
@"CREATE TABLE ""People"" (
""Id"" integer NOT NULL GENERATED ALWAYS AS IDENTITY
""Id"" integer GENERATED ALWAYS AS IDENTITY
);");
}

Expand Down Expand Up @@ -199,7 +199,7 @@ await Test(

AssertSql(
@"CREATE TABLE ""People"" (
""Id"" integer NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 10 INCREMENT BY 2 MAXVALUE 2000)
""Id"" integer GENERATED ALWAYS AS IDENTITY (START WITH 10 INCREMENT BY 2 MAXVALUE 2000)
);");
}

Expand Down Expand Up @@ -702,7 +702,7 @@ await Test(
});

AssertSql(
@"ALTER TABLE ""People"" ADD ""SomeColumn"" integer NULL GENERATED BY DEFAULT AS IDENTITY;");
@"ALTER TABLE ""People"" ADD ""SomeColumn"" integer GENERATED BY DEFAULT AS IDENTITY;");
}

[Fact]
Expand All @@ -727,7 +727,7 @@ await Test(
});

AssertSql(
@"ALTER TABLE ""People"" ADD ""SomeColumn"" integer NULL GENERATED ALWAYS AS IDENTITY;");
@"ALTER TABLE ""People"" ADD ""SomeColumn"" integer GENERATED ALWAYS AS IDENTITY;");
}

[Fact]
Expand Down Expand Up @@ -765,7 +765,7 @@ await Test(
});

AssertSql(
@"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);");
@"ALTER TABLE ""People"" ADD ""SomeColumn"" integer GENERATED BY DEFAULT AS IDENTITY (START WITH 5 INCREMENT BY 2 MINVALUE 3 MAXVALUE 2000 CYCLE CACHE 10);");
}

[Fact]
Expand Down Expand Up @@ -828,7 +828,7 @@ await Test(
});

AssertSql(
@"ALTER TABLE ""People"" ADD ""SomeColumn"" integer NOT NULL GENERATED BY DEFAULT AS IDENTITY;");
@"ALTER TABLE ""People"" ADD ""SomeColumn"" integer GENERATED BY DEFAULT AS IDENTITY;");
}

[Fact]
Expand Down

0 comments on commit 3317f9c

Please sign in to comment.