mirror of https://github.com/AMT-Cheif/drift.git
Web documentation for unique Dart columns
This commit is contained in:
parent
6047ec912f
commit
fb7246654d
|
@ -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
|
|
@ -1,7 +1,7 @@
|
||||||
---
|
---
|
||||||
data:
|
data:
|
||||||
title: "Dart tables"
|
title: "Dart tables"
|
||||||
description: Further information on Dart tables
|
description: Further information on defining tables in Dart.
|
||||||
weight: 150
|
weight: 150
|
||||||
template: layouts/docs/single
|
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 }}).
|
Drift's sql analyzer will generate matching Dart code. [Details]({{ "starting_with_sql.md" | pageUrl }}).
|
||||||
{% endblock %}
|
{% 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:
|
As shown in the [getting started guide]({{ "index.md" | pageUrl }}), sql tables can be written in Dart:
|
||||||
```dart
|
```dart
|
||||||
class Todos extends Table {
|
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 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
|
- 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
|
## Supported column types
|
||||||
|
|
||||||
Drift supports a variety of column types out of the box. You can store custom classes in columns by using
|
Drift supports a variety of column types out of the box. You can store custom classes in columns by using
|
||||||
|
|
Loading…
Reference in New Issue