Skip to content

Database integrity check passes when a declared column or index is missing from the database #5797

Closed

Description

@BiAksoy

Describe the bug

For managed tables, the startup integrity check only reports objects that exist in the live database and are absent from the models. The opposite case, a column, index or foreign key declared in the models but missing from the database, is never reported. The server starts normally and the problem surfaces at the first query that touches the missing column.

MigrationManager.verifyDatabaseIntegrity calls liveTable.like(table):

var mismatches =
(table.managed == false
? _compareUnmanagedColumns(table, liveTable)
: liveTable.like(table))
.asStringList();

TableComparisons.like iterates the columns, indexes and foreign keys of this, which here is the live table, and looks each one up in other, the target:

for (var column in columns) {
var otherColumn = other.findColumnNamed(column.name);
if (otherColumn == null) {
mismatches.add(
ColumnComparisonWarning(
name: column.name,
expected: column.name,
found: null,
),
);
} else {
var columnMismatches = column.like(otherColumn);
if (columnMismatches.isNotEmpty) {
mismatches.add(
ColumnComparisonWarning(
name: column.name,
).addSubs(columnMismatches),
);
}
}
}
for (var index in indexes) {
var otherIndex = other.findIndexNamed(index.indexName, ignoreCase: true);
if (otherIndex == null) {
mismatches.add(
IndexComparisonWarning(
name: index.indexName,
expected: index.indexName,
found: null,
),
);
} else {
var indexMismatches = index.like(otherIndex, ignoreCase: true);
if (indexMismatches.isNotEmpty) {
mismatches.add(
IndexComparisonWarning(
name: index.indexName,
).addSubs(indexMismatches),
);
}
}
}
for (var foreignKey in foreignKeys) {
var otherForeignKey = other.findForeignKeyDefinitionNamed(
foreignKey.constraintName,
ignoreCase: true,
);
if (otherForeignKey == null) {
mismatches.add(
ForeignKeyComparisonWarning(
name: foreignKey.constraintName,
expected: foreignKey.constraintName,
found: null,
),
);
} else {
var foreignKeyMismatches = foreignKey.like(
otherForeignKey,
ignoreCase: true,
);
if (foreignKeyMismatches.isNotEmpty) {
mismatches.add(
ForeignKeyComparisonWarning(

So only entries that exist in the live table are ever checked. The same inversion is why an extra index in the database is reported as Missing Index, which #5748 lists as "Mismatch warning reads backwards" (and #2879 before it). The example in #5748 shows both halves: the database has note_title_idx, the target wants note_title_body_idx, and the output names only note_title_idx. The index the target actually requires never appears.

Unmanaged tables are not affected, since #5756 compares them from the target side in _compareUnmanagedColumns.

To reproduce

Serverpod 4.0.0, embedded PostgreSQL.

  1. serverpod create probe --template server --no-auth --no-redis

  2. Add probe_server/lib/src/notes/note.spy.yaml:

    class: Note
    table: note
    fields:
      title: String
      body: String
    indexes:
      note_title_idx:
        fields: title
        unique: true
  3. serverpod generate && serverpod create-migration

  4. dart bin/main.dart --apply-migrations --role maintenance applies the migration, exit code 0.

  5. Remove a declared column and the declared index from the database:

    ALTER TABLE note DROP COLUMN body;
    DROP INDEX note_title_idx;
  6. Run dart bin/main.dart --apply-migrations --role maintenance again:

    SERVERPOD initialized, time: 2026-09-29 13:59:40.952366Z
    Latest database migration already applied.
    

    Exit code 0, no warning.

  7. For comparison, add an index the models do not declare, CREATE INDEX note_extra_idx ON note (title);, and run the same command:

    WARNING: The database does not match the target database:
     - Table "note" is not like the target database:
     - Missing Index "note_extra_idx".
    Hint: Did you forget to run `serverpod generate`, apply the migrations (--apply-migrations), or run a repair migration (--apply-repair-migration)?
    WARNING: Database does not match target state.
    

    Exit code 1. The extra index is caught, labelled as missing, and the missing body column and note_title_idx are still not mentioned.

The same holds on main (6e3b7ea) with SQLite. With the test harness in packages/serverpod_database/test/migration_manager_integrity_test.dart, a managed table declaring name and age against a live CREATE TABLE example (name TEXT NOT NULL) returns true with nothing logged, and so does a managed table declaring a unique index that the live table lacks.

Expected behavior

A column, index or foreign key declared in the models but absent from the database fails the integrity check and is reported as missing. An object that exists in the database but not in the models is reported as unexpected or extra rather than missing.

Library version

serverpod 4.0.0 (reproduced end to end), main at 6e3b7ea (reproduced with SQLite). Dart 3.13.4.

Platform information

macOS 27 (arm64), embedded PostgreSQL 16.13.

Additional context

  • bulk_data.dart makes the same call, liveTableDefinition.like(targetTableDefinition) (line 36), so it has the same blind spot.
  • expected and found are also swapped in the nested mismatch lines, since expected holds the live value.
  • Existing tests cover the extra-object direction for managed tables (an additional column, an undeclared expression index) and the missing-column direction only for unmanaged tables. There is no test for a managed table missing a declared column or index.
  • Fixing this makes startup fail on databases that pass today while missing a declared object. That is the purpose of the check, but it is worth a line in the changelog, because since fix: Exit with code 1 when the maintenance role fails to apply migrations #5719 the maintenance role also exits with 1 on a failed check outside development.
  • A possible fix is to iterate the target table's columns, indexes and foreign keys and report the ones missing from the live table as missing, then iterate the live table for extras. The warning classes already have an isAdded state for the second case.

Are you interested in working on a PR for this?

  • I want to work on this

Activity

  1. marcelomendoncasoares commented on Sep 29, 2026

    @marcelomendoncasoares
    Collaborator

    Hi @BiAksoy 👋

    Thanks for reporting! This is indeed a bug, as the integrity verification must report both ways - or the runtime protection fail. Surprising that this bug came this far without being reported, since it seems a regression introduced with #2778 (released on Serverpod 2.1.4). Previously, the bool check did compare both sides, but it contained no detailed description.

    Since you want to work on it, I'll suggest you keep the fix scoped to the check itself. We'll deal with the inverted expect/found separately.

  2. added
    bug
    area: migrations
    on Sep 29, 2026
  3. moved this from Contributor In Progress 👩‍💻 to Done 🚀 in Serverpod Frameworkson Oct 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area: migrationsbug

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions