Skip to content

Commit 2b0abd6

Browse files
Update schema.md field constraints list (#1069)
A reference to .sql() was added to the Field Constraint list so that anyone who is looking for any other options knows there is the possibility to use SQLKit.
1 parent 810f673 commit 2b0abd6

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

docs/fluent/schema.md

+24-23
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ try await database.schema("planets")
1111
.create()
1212
```
1313

14-
To create a `SchemaBuilder`, use the `schema` method on database. Pass in the name of the table or collection you want to affect. If you are editing the schema for a model, make sure this name matches the model's [`schema`](model.md#schema).
14+
To create a `SchemaBuilder`, use the `schema` method on database. Pass in the name of the table or collection you want to affect. If you are editing the schema for a model, make sure this name matches the model's [`schema`](model.md#schema).
1515

1616
## Actions
1717

18-
The schema API supports creating, updating, and deleting schemas. Each action supports a subset of the API's available methods.
18+
The schema API supports creating, updating, and deleting schemas. Each action supports a subset of the API's available methods.
1919

2020
### Create
2121

22-
Calling `create()` creates a new table or collection in the database. All methods for defining new fields and constraints are supported. Methods for updates or deletes are ignored.
22+
Calling `create()` creates a new table or collection in the database. All methods for defining new fields and constraints are supported. Methods for updates or deletes are ignored.
2323

2424
```swift
2525
// An example schema creation.
@@ -29,7 +29,7 @@ try await database.schema("planets")
2929
.create()
3030
```
3131

32-
If a table or collection with the chosen name already exists, an error will be thrown. To ignore this, use `.ignoreExisting()`.
32+
If a table or collection with the chosen name already exists, an error will be thrown. To ignore this, use `.ignoreExisting()`.
3333

3434
### Update
3535

@@ -54,14 +54,14 @@ database.schema("planets").delete()
5454

5555
## Field
5656

57-
Fields can be added when creating or updating a schema.
57+
Fields can be added when creating or updating a schema.
5858

5959
```swift
6060
// Adds a new field
6161
.field("name", .string, .required)
6262
```
6363

64-
The first parameter is the name of the field. This should match the key used on the associated model property. The second parameter is the field's [data type](#data-type). Finally, zero or more [constraints](#field-constraint) can be added.
64+
The first parameter is the name of the field. This should match the key used on the associated model property. The second parameter is the field's [data type](#data-type). Finally, zero or more [constraints](#field-constraint) can be added.
6565

6666
### Data Type
6767

@@ -86,13 +86,14 @@ Supported field data types are listed below.
8686

8787
### Field Constraint
8888

89-
Supported field constraints are listed below.
89+
Supported field constraints are listed below.
9090

9191
|FieldConstraint|Description|
9292
|-|-|
9393
|`.required`|Disallows `nil` values.|
94-
|`.references`|Requires that this field's value match a value in the referenced schema. See [foreign key](#foreign-key)|
95-
|`.identifier`|Denotes the primary key. See [identifier](#identifier)|
94+
|`.references`|Requires that this field's value match a value in the referenced schema. See [foreign key](#foreign-key).|
95+
|`.identifier`|Denotes the primary key. See [identifier](#identifier).|
96+
|`.sql(SQLColumnConstraintAlgorithm)`|Defines any constraint that is not supported (e.g. `default`). See [SQL](#sql) and [SQLColumnConstraintAlgorithm](https://api.vapor.codes/sqlkit/documentation/sqlkit/sqlcolumnconstraintalgorithm/).|
9697

9798
### Identifier
9899

@@ -103,18 +104,18 @@ If your model uses a standard `@ID` property, you can use the `id()` helper to c
103104
.id()
104105
```
105106

106-
For custom identifier types, you will need to specify the field manually.
107+
For custom identifier types, you will need to specify the field manually.
107108

108109
```swift
109110
// Adds field for custom identifier.
110111
.field("id", .int, .identifier(auto: true))
111112
```
112113

113-
The `identifier` constraint may be used on a single field and denotes the primary key. The `auto` flag determines whether or not the database should generate this value automatically.
114+
The `identifier` constraint may be used on a single field and denotes the primary key. The `auto` flag determines whether or not the database should generate this value automatically.
114115

115116
### Update Field
116117

117-
You can update a field's data type using `updateField`.
118+
You can update a field's data type using `updateField`.
118119

119120
```swift
120121
// Updates the field to `double` data type.
@@ -138,7 +139,7 @@ Constraints can be added when creating or updating a schema. Unlike [field const
138139

139140
### Unique
140141

141-
A unique constraint requires that there are no duplicate values in one or more fields.
142+
A unique constraint requires that there are no duplicate values in one or more fields.
142143

143144
```swift
144145
// Disallow duplicate email addresses.
@@ -152,7 +153,7 @@ If multiple field are constrained, the specific combination of each field's valu
152153
.unique(on: "first_name", "last_name")
153154
```
154155

155-
To delete a unique constraint, use `deleteUnique`.
156+
To delete a unique constraint, use `deleteUnique`.
156157

157158
```swift
158159
// Removes duplicate email constraint.
@@ -168,7 +169,7 @@ Fluent will generate unique constraint names by default. However, you may want t
168169
.unique(on: "email", name: "no_duplicate_emails")
169170
```
170171

171-
To delete a named constraint, you must use `deleteConstraint(name:)`.
172+
To delete a named constraint, you must use `deleteConstraint(name:)`.
172173

173174
```swift
174175
// Removes duplicate email constraint.
@@ -177,7 +178,7 @@ To delete a named constraint, you must use `deleteConstraint(name:)`.
177178

178179
## Foreign Key
179180

180-
Foreign key constraints require that a field's value match ones of the values in the referenced field. This is useful for preventing invalid data from being saved. Foreign key constraints can be added as either a field or top-level constraint.
181+
Foreign key constraints require that a field's value match ones of the values in the referenced field. This is useful for preventing invalid data from being saved. Foreign key constraints can be added as either a field or top-level constraint.
181182

182183
To add a foreign key constraint to a field, use `.references`.
183184

@@ -195,7 +196,7 @@ This same constraint could be added as a top-level constraint using `foreignKey`
195196
.foreignKey("star_id", references: "stars", "id")
196197
```
197198

198-
Unlike field constraints, top-level constraints can be added in a schema update. They can also be [named](#constraint-name).
199+
Unlike field constraints, top-level constraints can be added in a schema update. They can also be [named](#constraint-name).
199200

200201
Foreign key constraints support optional `onDelete` and `onUpdate` actions.
201202

@@ -215,7 +216,7 @@ Below is an example using foreign key actions.
215216
```
216217

217218
!!! warning
218-
Foreign key actions happen solely in the database, bypassing Fluent.
219+
Foreign key actions happen solely in the database, bypassing Fluent.
219220
This means things like model middleware and soft-delete may not work correctly.
220221

221222
## SQL
@@ -235,7 +236,7 @@ or even a default value for a timestamp:
235236

236237
## Dictionary
237238

238-
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.
239+
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.
239240

240241
!!! note
241242
Fluent's SQL database drivers store nested dictionaries in JSON columns.
@@ -262,15 +263,15 @@ This field can be stored using the `.dictionary(of:)` data type.
262263
.field("pet", .dictionary, .required)
263264
```
264265

265-
Since `Codable` types are heterogenous dictionaries, we do not specify the `of` parameter.
266+
Since `Codable` types are heterogenous dictionaries, we do not specify the `of` parameter.
266267

267268
If the dictionary values were homogenous, for example `[String: Int]`, the `of` parameter would specify the value type.
268269

269270
```swift
270271
.field("numbers", .dictionary(of: .int), .required)
271272
```
272273

273-
Dictionary keys must always be strings.
274+
Dictionary keys must always be strings.
274275

275276
## Array
276277

@@ -289,7 +290,7 @@ This field can be stored using the `.array(of:)` data type.
289290
.field("tags", .array(of: .string), .required)
290291
```
291292

292-
Since the array is homogenous, we specify the `of` parameter.
293+
Since the array is homogenous, we specify the `of` parameter.
293294

294295
Codable Swift `Array`s will always have a homogenous value type. Custom `Codable` types that serialize heterogenous values to unkeyed containers are the exception and should use the `.array` data type.
295296

@@ -391,7 +392,7 @@ struct UserNameMigration: AsyncMigration {
391392
try await User.query(on: database)
392393
.set(["first_name": .sql(embed: "name"))
393394
.run()
394-
395+
395396
try await database.schema("users")
396397
.deleteField("name")
397398
.update()

0 commit comments

Comments
 (0)