-
|
How would I make a nullable column non-nullable by inserting a default value? My new current schema looks like this: class Favorites extends Table {
TextColumn get productId => text()();
TextColumn get content => text().named('body')();
DateTimeColumn get createdAt => dateTime()();
IntColumn get rating => integer()();
BlobColumn get img => blob().nullable()();
}I added the img column and removed a My generated step-by-step migration looks like: stepByStep(
from1To2: (m, schema) async {m.alterTable(TableMigration(schema.favorites));
m.addColumn(schema.favorites, schema.favorites.img);
},
),The obvious solution of selecting everything to memory, pasting the non-null default, doing the schema migration, and then inserting the values seems overly complicated... |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
You can kind of optimize this (and use SQL tricks to avoid the "selecting everything to memory" part). You're already using the
I think that something like this can work here: stepByStep(
from1To2: (m, schema) async {
m.alterTable(TableMigration(schema.favorites, columnTransformer: {
schema.favorites.createdAt: currentDateAndTime,
schema.favorites.img: Constant('NULL'),
}, newColumns: [schema.favorites.img]));
},
), |
Beta Was this translation helpful? Give feedback.
You can kind of optimize this (and use SQL tricks to avoid the "selecting everything to memory" part). You're already using the
alterTableAPI which is the correct tool here, except that:alterTablealready adds new columns as well, so you don't need anaddColumnafterwards.I think that something like this can work here: