generatedAs method

VirtualColumnBuilder<T> generatedAs(
  1. Expression<T> generatedAs, {
  2. bool stored = false,
})

Declare a generated column.

Generated columns are backed by an expression, declared with generatedAs:

class Products extends Table {
  TextColumn get name => text()();

  RealColumn get price => real()();
  RealColumn get discount => real()();
  RealColumn get tax => real()();
  RealColumn get netPrice => real().generatedAs(
    price * (Constant(1) - discount) * (Constant(1) + tax))();
}

Generated columns can either be VIRTUAL (the default) or STORED (enabled with the stored parameter). Stored generated columns are computed on each update and are stored in the database. Virtual columns consume less space, but are re-computed on each read.

Generated columns can't be updated or inserted (neither with the Dart API or though SQL queries), so they are not represented in companions.

Important: When a generated column can be nullable, don't forget to call BuildGeneralColumn.nullable on it to reflect this in the generated code. Also, note that generated columns are part of your databases schema and cannot be updated easily. When changing the generatedAs expression for a column, you need to re-generate the table with a TableMigration.

Note that generated columns are only available in sqlite3 version 3.31.0. When using sqlite3_flutter_libs or a web database, this is not a problem.

Implementation

VirtualColumnBuilder<T> generatedAs(Expression<T> generatedAs,
        {bool stored = false}) =>
    _isGenerated();