From b43e93cb2a2631b4b5691fa972d17ea4bd2ee631 Mon Sep 17 00:00:00 2001 From: Paul Toffoloni <69189821+ptoffy@users.noreply.github.com> Date: Mon, 7 Aug 2023 17:56:29 +0200 Subject: [PATCH] Add default hint for required values in schema (#885) I've seen this asked more than once --------- Co-authored-by: Tim Condon <0xTim@users.noreply.github.com> Co-authored-by: Gwynne Raskind --- docs/fluent/schema.md | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/docs/fluent/schema.md b/docs/fluent/schema.md index 8d6f97323..f819f0566 100644 --- a/docs/fluent/schema.md +++ b/docs/fluent/schema.md @@ -218,6 +218,21 @@ Below is an example using foreign key actions. Foreign key actions happen solely in the database, bypassing Fluent. This means things like model middleware and soft-delete may not work correctly. +## SQL + +The `.sql` parameter allows you to add arbitrary SQL to your schema. This is useful for adding specific constraints or data types. +A common use case is defining a default value for a field: + +```swift +.field("active", .bool, .required, .sql(.default(true))) +``` + +or even a default value for a timestamp: + +```swift +.field("created_at", .datetime, .required, .sql(.default(SQLFunction("now")))) +``` + ## Dictionary The dictionary data type is capable of storing nested dictionary values. This includes structs that conform to `Codable` and Swift dictionaries with a `Codable` value. @@ -389,4 +404,4 @@ try await db.schema("planets", space: "mirror_universe") .id() // ... .create() -``` \ No newline at end of file +```