Skip to content

A table moved back to the default schema stays where it was on Npgsql, and a statement written after the move fails with 42P01 #105

Done

Description

@CaffeinatedCoder

Found by the pre-preview sweep of main @ 948496a (lens: base operations); reproduced on PostgreSQL 16 and 18.

What happens

A table moves from a schema of its own back to the default one: ToTable("role_grants", "grants") becomes ToTable("role_grants"), or HasDefaultSchema("app") is removed. This is also the Down of every move into a schema.

  • With no SchemaKit declaration at all, the migration applies and the table stays in grants. Npgsql 11.0.0-rc.3 writes ALTER TABLE … SET SCHEMA only when RenameTableOperation.NewSchema is not null (decompiled NpgsqlMigrationsSqlGenerator.Generate(RenameTableOperation)).
  • With an exclusion constraint that stays as it is, the constraint stays with the table, in grants. EnsureCreated() for the same model puts both in public.
  • With one that is rebuilt in the same migration, the create names the table without a schema and fails: 42P01: relation "role_grants" does not exist.

The planner records TableMoved(grants → null) and takes the table for moved.

Whose it is

Npgsql's. SchemaKit writes nothing wrong for the model it is given, and where it writes nothing the database differs from the model without any package noticing. It is tracked here because a consumer meets it through SchemaKit's statement, and because the move forward is a case the live tests hold (Moving_the_table_to_another_schema) while the way back is not.

Change

Test

AssertMigratesTo<Models.SchedulingMoved, Models.Scheduling> in PgExclusionLiveTests, which fails today on the schema in the catalog, kept as the measure of the upstream fix.

Activity

  1. added this to the 11.0.0 milestone on Nov 14, 2026
  2. added
    bug
    package/postgresql
    severity/medium
    area/migrations
    on Nov 14, 2026
  3. CaffeinatedCoder commented on Nov 25, 2026

    @CaffeinatedCoder
    OwnerAuthor

    2026-11-25: still so on the released Npgsql.EntityFrameworkCore.PostgreSQL 11.0.0, and SchemaKit refuses it now (#198, decided with #116). With an object on the table the migration is refused where it is scaffolded, and the refusal says to name the schema, ToTable("x", "public"), which Npgsql writes. NpgsqlOperationEffectTests.A_move_back_to_the_default_schema_is_not_written_and_the_dialect_says_so asserts that the generator writes nothing, and fails when it does: the rule and the refusal then go.

    A reproduction for the report upstream (npgsql/efcore.pg), with no SchemaKit in it:

    using Microsoft.EntityFrameworkCore;
    using Microsoft.EntityFrameworkCore.Infrastructure;
    using Microsoft.EntityFrameworkCore.Migrations;
    using Microsoft.EntityFrameworkCore.Migrations.Operations;
    
    using var context = new Db();
    var generator = context.GetService<IMigrationsSqlGenerator>();
    
    string Sql(string? from, string? to) => string.Join("\n", generator
        .Generate([new RenameTableOperation { Name = "role_grants", Schema = from, NewName = "role_grants", NewSchema = to }])
        .Select(command => command.CommandText));
    
    Console.WriteLine($"into a schema: [{Sql(null, "grants")}]");   // ALTER TABLE role_grants SET SCHEMA grants;
    Console.WriteLine($"back:          [{Sql("grants", null)}]");   // nothing
    
    sealed class Db : DbContext
    {
        protected override void OnConfiguring(DbContextOptionsBuilder options) => options.UseNpgsql("Host=localhost");
    }

    What a consumer sees: ToTable("role_grants", "grants") becomes ToTable("role_grants"), migrations add scaffolds RenameTable(name: "role_grants", schema: "grants", newName: "role_grants"), the migration applies, and the table is still in grants. It is also the Down of every move into a schema. Expected: ALTER TABLE grants.role_grants SET SCHEMA public;, or the schema the model's default is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/migrationsbugpackage/postgresqlseverity/medium

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions