diff --git a/moor/lib/src/runtime/structure/columns.dart b/moor/lib/src/runtime/structure/columns.dart index df009c5a..3d7ed81c 100644 --- a/moor/lib/src/runtime/structure/columns.dart +++ b/moor/lib/src/runtime/structure/columns.dart @@ -27,8 +27,15 @@ abstract class GeneratedColumn> extends Column { /// [here](https://www.sqlite.org/syntax/column-def.html), into the given /// buffer. void writeColumnDefinition(StringBuffer into) { - into..write('${$name} $typeName ')..write($nullable ? 'NULL' : 'NOT NULL'); - writeCustomConstraints(into); + into.write('${$name} $typeName '); + + if ($customConstraints == null) { + into.write($nullable ? 'NULL' : 'NOT NULL'); + // these custom constraints refer to builtin constraints from moor + writeCustomConstraints(into); + } else { + into.write($customConstraints); + } } @visibleForOverriding diff --git a/moor/test/data/tables/todos.dart b/moor/test/data/tables/todos.dart index 9e3e1b2b..04a28dd7 100644 --- a/moor/test/data/tables/todos.dart +++ b/moor/test/data/tables/todos.dart @@ -26,7 +26,8 @@ class Users extends Table { @DataClassName('Category') class Categories extends Table { IntColumn get id => integer().autoIncrement()(); - TextColumn get description => text().named('desc')(); + TextColumn get description => + text().named('desc').customConstraint('NOT NULL UNIQUE')(); } class SharedTodos extends Table { diff --git a/moor/test/data/tables/todos.g.dart b/moor/test/data/tables/todos.g.dart index ecd7d6fd..53c71098 100644 --- a/moor/test/data/tables/todos.g.dart +++ b/moor/test/data/tables/todos.g.dart @@ -217,10 +217,9 @@ class $CategoriesTable extends Categories _id ??= GeneratedIntColumn('id', false, hasAutoIncrement: true); GeneratedTextColumn _description; @override - GeneratedTextColumn get description => _description ??= GeneratedTextColumn( - '`desc`', - false, - ); + GeneratedTextColumn get description => + _description ??= GeneratedTextColumn('`desc`', false, + $customConstraints: 'NOT NULL UNIQUE'); @override List get $columns => [id, description]; @override diff --git a/moor/test/schema_test.dart b/moor/test/schema_test.dart index 8bfcb181..2aebfd38 100644 --- a/moor/test/schema_test.dart +++ b/moor/test/schema_test.dart @@ -24,7 +24,7 @@ void main() { 'category INTEGER NULL);')); verify(mockQueryExecutor.call('CREATE TABLE IF NOT EXISTS categories ' - '(id INTEGER PRIMARY KEY AUTOINCREMENT, `desc` VARCHAR NOT NULL);')); + '(id INTEGER PRIMARY KEY AUTOINCREMENT, `desc` VARCHAR NOT NULL UNIQUE);')); verify(mockQueryExecutor.call('CREATE TABLE IF NOT EXISTS users ' '(id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR NOT NULL, ' diff --git a/moor_generator/lib/src/writer/table_writer.dart b/moor_generator/lib/src/writer/table_writer.dart index 46904c4b..8ab38792 100644 --- a/moor_generator/lib/src/writer/table_writer.dart +++ b/moor_generator/lib/src/writer/table_writer.dart @@ -98,6 +98,11 @@ class TableWriter { } } + if (column.customConstraints != null) { + // todo should we worry about escaping this string? + additionalParams['\$customConstraints'] = "'${column.customConstraints}'"; + } + // GeneratedIntColumn('sql_name', isNullable, additionalField: true) final expressionBuffer = StringBuffer() ..write(column.implColumnTypeName)