entity framework core npgsql migration alter sequence error

Viewed 470

I have a migration like this

migrationBuilder.AlterColumn<int>(
    name: "Id",
    schema: "{table-schema}",
    table: "{table-name}",
    nullable: false,
    oldClrType: typeof(int))
    .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.SerialColumn);

It produces a sql like this

DO $$
BEGIN
    IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '{migration-name}') THEN
        ALTER TABLE {table-schema}."{table-name}" ALTER COLUMN "Id" TYPE integer;
        ALTER TABLE {table-schema}."{table-name}" ALTER COLUMN "Id" SET NOT NULL;
        CREATE SEQUENCE "{table-schema}_Id_seq" AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE NO CYCLE;
    END IF;
END $$;

followed by

DO $$
BEGIN
    IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '{migration-name') THEN
        ALTER TABLE {table-schema}."{migration-name}" ALTER COLUMN "Id" SET DEFAULT (nextval('"{table-name}_Id_seq"'));
        ALTER SEQUENCE "{table-name}_Id_seq" OWNED BY "{table-name}"."Id";
    END IF;
END $$;

I get the following error during migration: System.Exception: 42P01: relation "{table-name}" does not exist.

What could be wrong here?

When I run the sql manually, I get the error at the ALTER SEQUENCE line. I am using npgsql provider 2.1.2 and tried it for both postgresql 9.5 and 11.

0 Answers
Related