compileColumn static method

String compileColumn(
  1. MigrationColumn column, {
  2. bool constraints = true,
})

The definition of column, after its name. A serial primary key is an INTEGER PRIMARY KEY AUTOINCREMENT, whose ids are never used again.

Without constraints, it has no UNIQUE and no PRIMARY KEY, which SQLite does not add to a table, and throws an UnsupportedError for a primary key.

Implementation

static String compileColumn(
  MigrationColumn column, {
  bool constraints = true,
}) {
  var buf = StringBuffer(columnType(column));
  var serial = sql.serialIntegers.containsKey(column.type);

  if (column.indexType == IndexType.primaryKey) {
    if (!constraints) {
      throw UnsupportedError(
        'SQLite cannot add a PRIMARY KEY column to a table.',
      );
    }
    buf.write(' PRIMARY KEY');
    if (serial) buf.write(' AUTOINCREMENT');
  }
  if (column.isNullable == false) buf.write(' NOT NULL');
  var defaultValue = column.defaultValue;
  if (defaultValue != null) {
    buf.write(' DEFAULT ${compileDefault(defaultValue)}');
  }
  if (constraints && column.indexType == IndexType.unique) {
    buf.write(' UNIQUE');
  }

  for (var ref in column.externalReferences) {
    buf.write(' ${compileReference(ref)}');
  }

  return buf.toString();
}