Skip to content

Brick generates indexes for columns that were removed, causing migration crash #665

@KiddoV

Description

@KiddoV

When a model field is removed, Brick still generates migrations that attempt to create indexes for the old column. This causes SQLite to crash during initialization with no such column errors.

Field removed was

  //
  @Supabase(foreignKey: "recipient_id", ignoreTo: true)
  @Sqlite(index: true)
  final Person? recipient;
  @Sqlite(ignore: true)
  String? get recipientId => recipient?.id;
  @Supabase(foreignKey: "recipient_group_id", ignoreTo: true)
  @Sqlite(index: true)
  final Group? recipientGroup;
  @Sqlite(ignore: true)
  String? get recipientGroupId => recipientGroup?.id;
  //

In the new migration Brick still add the indexes after already DropColumn

const List<MigrationCommand> _migration_20260119133856_up = [
  DropColumn('recipient_Person_brick_id', onTable: 'Chat'),
  DropColumn('recipient_group_Group_brick_id', onTable: 'Chat'),
  CreateIndex(columns: ['recipient_Person_brick_id'], onTable: 'Chat', unique: false),
  CreateIndex(columns: ['recipient_group_Group_brick_id'], onTable: 'Chat', unique: false)
]

Which cause my app crash...

flutter: \^[[38;5;1m┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────\^[[0m
flutter: \^[[38;5;1m│ [exception] | 18:47:29 44ms | [DATABASE] Error initialize app database\^[[0m
flutter: \^[[38;5;1m│ DatabaseException(Error Domain=FMDatabase Code=1 "no such column: recipient_Person_brick_id" UserInfo={NSLocalizedDescription=no such column: recipient_Person_brick_id}) sql 'CREATE INDEX IF NOT EXISTS index_Chat_on_recipient_Person_brick_id on `Chat`(`recipient_Person_brick_id`)' args []\^[[0m
flutter: \^[[38;5;1m│ StackTrace: #0      SqfliteSqlCipherDatabaseFactoryImpl.wrapDatabaseException (package:sqflite_sqlcipher/src/factory_sql_cipher_impl.dart:44:9)\^[[0m
flutter: \^[[38;5;1m│ <asynchronous suspension>\^[[0m

Is this a bug?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions