From e0ce0933619845f5fa5301ad66d809bf5fd8a497 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Mon, 13 Jul 2020 15:14:45 +0200 Subject: [PATCH] Explain @required in data classes (#548) --- .../content/en/docs/Getting started/advanced_dart_tables.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/docs/content/en/docs/Getting started/advanced_dart_tables.md b/docs/content/en/docs/Getting started/advanced_dart_tables.md index 26974c5c..d638e1d5 100644 --- a/docs/content/en/docs/Getting started/advanced_dart_tables.md +++ b/docs/content/en/docs/Getting started/advanced_dart_tables.md @@ -69,13 +69,17 @@ be used when inserting a new row. To set a constant default value, use `withDefa ```dart class Preferences extends Table { - TextColumn get name => integer().autoIncrement()(); + TextColumn get name => text()(); BoolColumn get enabled => boolean().withDefault(const Constant(false))(); } ``` When you later use `into(preferences).insert(PreferencesCompanion.forInsert(name: 'foo'));`, the new row will have its `enabled` column set to false (and not to null, as it normally would). +Note that columns with a default value (either through `autoIncrement` or by using a default), are +still marked as `@required` in generated data classes. This is because they are meant to represent a +full row, and every row will have those values. Use companions when representing partial rows, like +for inserts or updates. Of course, constants can only be used for static values. But what if you want to generate a dynamic default value for each column? For that, you can use `clientDefault`. It takes a function returning