迁移实体并添加列后,我想在表中为新添加的列插入已存在记录的值。我怎样才能做到这一点?
例如,在这种情况下,对于数据库中已经存在的记录,我希望 DueDate 列的值为 DateTime(2019,1,1)。
class Todos extends Table {
IntColumn get id => integer().autoIncrement()();
TextColumn get title => text().withLength(min: 6, max: 10)();
TextColumn get content => text().named('body')();
IntColumn get category => integer().nullable()();
DateTimeColumn get dueDate => dateTime().nullable()(); // new, added column
}
int get schemaVersion => 2; // bump because the tables have changed
@override
MigrationStrategy get migration => MigrationStrategy(
onCreate: (Migrator m) {
return m.createAllTables();
},
onUpgrade: (Migrator m, int from, int to) async {
if (from == 1) {
// we added the dueDate property in the change from version 1
await m.addColumn(todos, todos.dueDate);
}
}
);