@@ -15,7 +15,7 @@ We must then define a _schema class_. This is an `abstract final class`
1515extending [ Schema] , specifying what tables the database has.
1616If we were working on a bookstore we might define a schema as follows:
1717
18- ``` dart schema_test .dart#schema
18+ ``` dart schema_references_test .dart#schema
1919abstract final class Bookstore extends Schema {
2020 Table<Author> get authors;
2121 Table<Book> get books;
@@ -37,7 +37,7 @@ For each table in our database we must define a _row class_. This is an
3737the database table has. Continuing with the bookstore example we can define a
3838_ row class_ for the ` authors ` table as follows:
3939
40- ``` dart schema_test .dart#author-model
40+ ``` dart schema_references_test .dart#author-model
4141@PrimaryKey(['authorId'])
4242abstract final class Author extends Row {
4343 @AutoIncrement()
@@ -95,7 +95,7 @@ _row class_ for the `books` table in the `Bookstore` schema. If we want the
9595` books ` table to have a _ foreign key_ referencing the ` authors ` table we can
9696define the ` Book ` _ row class_ as follows:
9797
98- ``` dart schema_test .dart#book-model
98+ ``` dart schema_references_test .dart#book-model
9999@PrimaryKey(['bookId'])
100100abstract final class Book extends Row {
101101 @AutoIncrement()
@@ -123,9 +123,6 @@ abstract final class Book extends Row {
123123}
124124```
125125
126- The ` @DefautValue(0) ` annotation gives the ` stock ` field a default value of ` 0 ` .
127- This also makes the ` stock ` field optional when inserting rows.
128-
129126> [ !NOTE]
130127> The ` name ` and ` as ` properties in the ` @References ` annotation are optional.
131128> These gives rise to convinient subquery properties we can use when writing
@@ -145,6 +142,53 @@ CREATE TABLE books (
145142Notice that because the ` title ` field is nullable, it does not have a ` NOT NULL `
146143constraint in the database.
147144
145+ ## Adding a default value
146+ In our bookstore example above, the _ row class_ ` Book ` has a ` stock ` field
147+ annotated with ` @DefaultValue(0) ` . This gives the ` stock ` field a default value
148+ of ` 0 ` , and thus, causes the ` stock ` field to be optional when inserting rows.
149+
150+ ``` dart schema_default_value_test.dart#book-model
151+ @PrimaryKey(['bookId'])
152+ abstract final class Book extends Row {
153+ @AutoIncrement()
154+ int get bookId;
155+
156+ String? get title;
157+
158+ @References(table: 'authors', field: 'authorId', name: 'author', as: 'books')
159+ int get authorId;
160+
161+ @DefaultValue(0) // Gives the `stock` field to have a default value!
162+ int get stock;
163+ }
164+ ```
165+
166+ The equivalent SQL depends on the database, but it looks something like:
167+ ``` sql
168+ CREATE TABLE books (
169+ bookId BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY ,
170+ title TEXT ,
171+ authorId BIGINT NOT NULL ,
172+ stock BIGINT NOT NULL DEFAULT 0 ,
173+ FOREIGN KEY (authorId) REFERENCES authors (authorId)
174+ );
175+ ```
176+
177+ Notice the ` DEFAULT 0 ` clause in the declaration of the ` stock ` field.
178+
179+ It is possible to define default values for ` String ` , ` bool ` , ` int ` and ` double `
180+ fields, using the ` @DefaultValue(...) ` annotation. However, since [ DateTime]
181+ does not have a _ const_ constructor one of the following annoations must be
182+ used to create a [ DateTime] field with a _ default value_ :
183+ * ` @DefaultValue.dateTime(year, [month, day, ...]) ` ,
184+ * ` @DefaultValue.epoch ` , or,
185+ * ` @DefaultValue.now ` .
186+
187+ With [ DefaultValue.now] being a special constructor that uses
188+ ` CURRENT_TIMESTAMP ` in the database to ensure the field
189+ has a default value of ` DateTime.now().toUtc() ` when row is inserted.
190+
191+ See [ DefaultValue] for further documentation on _ default values_ .
148192
149193## Generating code
150194Whenever the definition of the database schema is changed, it's important to
@@ -177,7 +221,7 @@ your database, as well as how you are connecting.
177221
178222Once you have ` Database<Bookstore> ` instance you can create empty tables for
179223your schema as follows:
180- ``` dart schema_test .dart#create-tables
224+ ``` dart schema_references_test .dart#create-tables
181225// Create tables
182226await db.createTables();
183227```
@@ -186,7 +230,7 @@ Creating empty tables from scratch is mostly useful for testing, it's rarely
186230needed in production. Instead you can use the generated ` create<Schema>Tables ` ,
187231which outputs the [ DDL] for creating the tables.
188232
189- ``` dart schema_test .dart#get-ddl
233+ ``` dart schema_references_test .dart#get-ddl
190234// Get the database schema
191235final ddl = createBookstoreTables(SqlDialect.postgres());
192236```
@@ -204,7 +248,7 @@ database migrations. See [Migrations documentation][Migrations].
204248With a ` Database<Bookstore> ` and tables created through migrations or
205249` db.createTables() ` you can insert data into the database as follows:
206250
207- ``` dart schema_test .dart#insert-data
251+ ``` dart schema_references_test .dart#insert-data
208252// Insert a row into the "authors" table
209253final author = await db.authors
210254 .insert(
@@ -226,7 +270,7 @@ Now we can also write queries against the database. The following demonstrates
226270how to write a query that filters on the book title and only returns ` title `
227271and ` author.name ` .
228272
229- ``` dart schema_test .dart#query-data
273+ ``` dart schema_references_test .dart#query-data
230274// Query for books where the title contains 'eggs'
231275// select the title and author name
232276final titleAndAuthor = await db.books
0 commit comments