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

[EFCore, Npgsql] ValueGeneratedOnAdd being incorrectly generated by Migration? #16628

Open
JakenVeina opened this issue Jul 17, 2019 · 2 comments

Comments

@JakenVeina
Copy link

commented Jul 17, 2019

The designer seems to be generating an incorrect ModelSnapshot for me, which results in an incorrect migration.

When generating a new migration, with no changes at all to source, I end up with this:

protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.AlterColumn<long>(
        name: "Id",
        table: "Users",
        nullable: false,
        oldClrType: typeof(long))
        .OldAnnotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.SerialColumn);
}

protected override void Down(MigrationBuilder migrationBuilder)
{
    migrationBuilder.AlterColumn<long>(
        name: "Id",
        table: "Users",
        nullable: false,
        oldClrType: typeof(long))
        .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.SerialColumn);
}

Running this produces the following exception, due to the fact that the proposed sequence doesn't exist.

Applying migration '20190716015043_Initial'.
Applying migration '20190717003909_test'.
Failed executing DbCommand (15ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
ALTER TABLE "Users" ALTER COLUMN "Id" TYPE bigint;
ALTER TABLE "Users" ALTER COLUMN "Id" SET NOT NULL;
DROP SEQUENCE "Users_Id_seq" CASCADE;
ALTER TABLE "Users" ALTER COLUMN "Id" DROP DEFAULT;
Npgsql.PostgresException (0x80004005): 42P01: sequence "Users_Id_seq" does not exist
   at Npgsql.NpgsqlConnector.<>c__DisplayClass161_0.<<ReadMessage>g__ReadMessageLong|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Npgsql.NpgsqlConnector.<>c__DisplayClass161_0.<<ReadMessage>g__ReadMessageLong|0>d.MoveNext() in C:\projects\npgsql\src\Npgsql\NpgsqlConnector.cs:line 1032
--- End of stack trace from previous location where exception was thrown ---
   at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming) in C:\projects\npgsql\src\Npgsql\NpgsqlDataReader.cs:line 490
   at Npgsql.NpgsqlDataReader.NextResult() in C:\projects\npgsql\src\Npgsql\NpgsqlDataReader.cs:line 332
   at Npgsql.NpgsqlCommand.ExecuteDbDataReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken) in C:\projects\npgsql\src\Npgsql\NpgsqlCommand.cs:line 1218
   at Npgsql.NpgsqlCommand.ExecuteNonQuery(Boolean async, CancellationToken cancellationToken) in C:\projects\npgsql\src\Npgsql\NpgsqlCommand.cs:line 1042
   at Npgsql.NpgsqlCommand.ExecuteNonQuery() in C:\projects\npgsql\src\Npgsql\NpgsqlCommand.cs:line 1025
   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues)
   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQuery(IEnumerable`1 migrationCommands, IRelationalConnection connection)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
42P01: sequence "Users_Id_seq" does not exist

Looking at the Initial migration for those table, I can see that no generation is specified.

migrationBuilder.CreateTable(
    name: "Users",
    columns: table => new
    {
        Id = table.Column<long>(nullable: false),
        Username = table.Column<string>(nullable: false),
        Discriminator = table.Column<string>(nullable: false),
        AvatarHash = table.Column<string>(nullable: true),
        FirstSeen = table.Column<DateTimeOffset>(nullable: false),
        LastSeen = table.Column<DateTimeOffset>(nullable: false)
    },
    constraints: table =>
    {
        table.PrimaryKey("PK_Users", x => x.Id);
    });

However, in the ModelSnapshot both before and after adding the test migration, I see that a generator is specified.

modelBuilder.Entity("Sokan.Yastah.Data.Users.UserEntity", b =>
    {
        b.Property<long>("Id")
            .ValueGeneratedOnAdd();

        b.Property<string>("AvatarHash");

        b.Property<string>("Discriminator")
            .IsRequired();

        b.Property<DateTimeOffset>("FirstSeen");

        b.Property<DateTimeOffset>("LastSeen");

        b.Property<string>("Username")
            .IsRequired();

        b.HasKey("Id");

        b.ToTable("Users");
    });

The full definition for UserEntity can be found in the repository below.

Deleting and re-creating all migrations produces the same result.

Full reproduction repository

Microsoft.EntityFrameworkCore v2.2.6
Microsoft.EntityFrameworkCore.Design v2.2.6
Npgsql.EntityFrameworkCore.PostgreSQL v2.2.4
Microsoft.NETCore.App v2.2.0
Visual Studio 2019 Community 16.1.1

JakenVeina added a commit to JakenVeina/Sokan.Yastah that referenced this issue Jul 17, 2019
@JakenVeina

This comment has been minimized.

Copy link
Author

commented Jul 17, 2019

Also, I just noticed that there are two other entity types for which a value generator is being added, despite it not being in the model. PermissionCategoryEntity and PermissionEntity. These are presenting slightly differently, though, in that the Initial migration is self-consistent (it specifies that value generation is added). For these tables, running the migration doesn't result in an error, because a sequence is actually created during the Initial migration. It's just that a sequence shouldn't have been generated.

@JakenVeina JakenVeina changed the title [EFCore] ValueGeneratedOnAdd being incorrectly generated by Migration? [EFCore, Npgsql] ValueGeneratedOnAdd being incorrectly generated by Migration? Jul 17, 2019
JakenVeina added a commit to JakenVeina/Sokan.Yastah that referenced this issue Jul 17, 2019
@ajcvickers

This comment has been minimized.

Copy link
Member

commented Jul 17, 2019

@roji Does this look like it could be an Npgsql provider issue? Or more likely a core issue?

@ajcvickers ajcvickers added this to the Backlog milestone Jul 29, 2019
JakenVeina added a commit to JakenVeina/Sokan.Yastah that referenced this issue Aug 27, 2019
JakenVeina added a commit to JakenVeina/Sokan.Yastah that referenced this issue Aug 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.