You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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).
15
15
16
16
## Actions
17
17
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.
19
19
20
20
### Create
21
21
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.
Fields can be added when creating or updating a schema.
57
+
Fields can be added when creating or updating a schema.
58
58
59
59
```swift
60
60
// Adds a new field
61
61
.field("name", .string, .required)
62
62
```
63
63
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.
65
65
66
66
### Data Type
67
67
@@ -86,13 +86,14 @@ Supported field data types are listed below.
86
86
87
87
### Field Constraint
88
88
89
-
Supported field constraints are listed below.
89
+
Supported field constraints are listed below.
90
90
91
91
|FieldConstraint|Description|
92
92
|-|-|
93
93
|`.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/).|
96
97
97
98
### Identifier
98
99
@@ -103,18 +104,18 @@ If your model uses a standard `@ID` property, you can use the `id()` helper to c
103
104
.id()
104
105
```
105
106
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.
107
108
108
109
```swift
109
110
// Adds field for custom identifier.
110
111
.field("id", .int, .identifier(auto: true))
111
112
```
112
113
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.
114
115
115
116
### Update Field
116
117
117
-
You can update a field's data type using `updateField`.
118
+
You can update a field's data type using `updateField`.
118
119
119
120
```swift
120
121
// Updates the field to `double` data type.
@@ -138,7 +139,7 @@ Constraints can be added when creating or updating a schema. Unlike [field const
138
139
139
140
### Unique
140
141
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.
142
143
143
144
```swift
144
145
// Disallow duplicate email addresses.
@@ -152,7 +153,7 @@ If multiple field are constrained, the specific combination of each field's valu
152
153
.unique(on: "first_name", "last_name")
153
154
```
154
155
155
-
To delete a unique constraint, use `deleteUnique`.
156
+
To delete a unique constraint, use `deleteUnique`.
156
157
157
158
```swift
158
159
// Removes duplicate email constraint.
@@ -168,7 +169,7 @@ Fluent will generate unique constraint names by default. However, you may want t
168
169
.unique(on: "email", name: "no_duplicate_emails")
169
170
```
170
171
171
-
To delete a named constraint, you must use `deleteConstraint(name:)`.
172
+
To delete a named constraint, you must use `deleteConstraint(name:)`.
172
173
173
174
```swift
174
175
// Removes duplicate email constraint.
@@ -177,7 +178,7 @@ To delete a named constraint, you must use `deleteConstraint(name:)`.
177
178
178
179
## Foreign Key
179
180
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.
181
182
182
183
To add a foreign key constraint to a field, use `.references`.
183
184
@@ -195,7 +196,7 @@ This same constraint could be added as a top-level constraint using `foreignKey`
195
196
.foreignKey("star_id", references: "stars", "id")
196
197
```
197
198
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).
199
200
200
201
Foreign key constraints support optional `onDelete` and `onUpdate` actions.
201
202
@@ -215,7 +216,7 @@ Below is an example using foreign key actions.
215
216
```
216
217
217
218
!!! warning
218
-
Foreign key actions happen solely in the database, bypassing Fluent.
219
+
Foreign key actions happen solely in the database, bypassing Fluent.
219
220
This means things like model middleware and soft-delete may not work correctly.
220
221
221
222
## SQL
@@ -235,7 +236,7 @@ or even a default value for a timestamp:
235
236
236
237
## Dictionary
237
238
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.
239
240
240
241
!!! note
241
242
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.
262
263
.field("pet", .dictionary, .required)
263
264
```
264
265
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.
266
267
267
268
If the dictionary values were homogenous, for example `[String: Int]`, the `of` parameter would specify the value type.
@@ -289,7 +290,7 @@ This field can be stored using the `.array(of:)` data type.
289
290
.field("tags", .array(of: .string), .required)
290
291
```
291
292
292
-
Since the array is homogenous, we specify the `of` parameter.
293
+
Since the array is homogenous, we specify the `of` parameter.
293
294
294
295
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.
0 commit comments