Web documentation for unique Dart columns

This commit is contained in:
Simon Binder 2022-04-03 12:44:02 +02:00
parent 6047ec912f
commit fb7246654d
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
2 changed files with 33 additions and 1 deletions

View File

@ -0,0 +1,17 @@
import 'package:drift/drift.dart';
// #docregion unique
class WithUniqueConstraints extends Table {
IntColumn get a => integer().unique()();
IntColumn get b => integer()();
IntColumn get c => integer()();
@override
List<Set<Column>> get uniqueKeys => [
{b, c}
];
// Effectively, this table has two unique key sets: (a) and (b, c).
}
// #enddocregion unique

View File

@ -1,7 +1,7 @@
---
data:
title: "Dart tables"
description: Further information on Dart tables
description: Further information on defining tables in Dart.
weight: 150
template: layouts/docs/single
---
@ -11,6 +11,8 @@ __Prefer sql?__ If you prefer, you can also declare tables via `CREATE TABLE` st
Drift's sql analyzer will generate matching Dart code. [Details]({{ "starting_with_sql.md" | pageUrl }}).
{% endblock %}
{% assign snippets = 'package:moor_documentation/snippets/tables/advanced.dart.excerpt.json' | readString | json_decode %}
As shown in the [getting started guide]({{ "index.md" | pageUrl }}), sql tables can be written in Dart:
```dart
class Todos extends Table {
@ -128,6 +130,19 @@ Note that the primary key must essentially be constant so that the generator can
- it must be defined with the `=>` syntax, function bodies aren't supported
- it must return a set literal without collection elements like `if`, `for` or spread operators
## Unique Constraints
Starting from version 1.6.0, `UNIQUE` SQL constraints can be defined on Dart tables too.
A unique constraint contains one or more columns. The combination of all columns in a constraint
must be unique in the table, or the databas will report an error on inserts.
With drift, a unique constraint can be added to a single column by marking it as `.unique()` in
the column builder.
A unique set spanning multiple columns can be added by overriding the `uniqueKeys` getter in the
`Table` class:
{% include "blocks/snippet" snippets = snippets name = 'unique' %}
## Supported column types
Drift supports a variety of column types out of the box. You can store custom classes in columns by using