|
1 | 1 | extension PostgreSQLQuery {
|
| 2 | + /// In addition, when the data in the referenced columns is changed, certain actions are performed on the data in this table's |
| 3 | + /// columns. The ON DELETE clause specifies the action to perform when a referenced row in the referenced table is being deleted. |
| 4 | + /// Likewise, the ON UPDATE clause specifies the action to perform when a referenced column in the referenced table is being updated |
| 5 | + /// to a new value. If the row is updated, but the referenced column is not actually changed, no action is done. Referential actions |
| 6 | + /// other than the NO ACTION check cannot be deferred, even if the constraint is declared deferrable. There are the following possible |
| 7 | + /// actions for each clause. |
| 8 | + /// |
| 9 | + /// https://www.postgresql.org/docs/10/static/sql-createtable.html. |
2 | 10 | public enum ForeignKeyAction {
|
3 |
| - case nullify |
| 11 | + /// Produce an error indicating that the deletion or update would create a foreign key constraint violation. |
| 12 | + /// If the constraint is deferred, this error will be produced at constraint check time if there still exist |
| 13 | + /// any referencing rows. This is the default action. |
| 14 | + case noAction |
| 15 | + /// Produce an error indicating that the deletion or update would create a foreign key constraint violation. |
| 16 | + /// This is the same as NO ACTION except that the check is not deferrable. |
| 17 | + case restrict |
| 18 | + /// Delete any rows referencing the deleted row, or update the values of the referencing column(s) to the new |
| 19 | + // values of the referenced columns, respectively. |
| 20 | + case cascade |
| 21 | + /// Set the referencing column(s) to null. |
| 22 | + case setNull |
| 23 | + /// Set the referencing column(s) to their default values. (There must be a row in the referenced table matching |
| 24 | + /// the default values, if they are not null, or the operation will fail.) |
| 25 | + case setDefault |
4 | 26 | }
|
5 | 27 | }
|
6 | 28 |
|
7 | 29 | extension PostgreSQLSerializer {
|
8 | 30 | internal func serialize(_ action: PostgreSQLQuery.ForeignKeyAction) -> String {
|
9 | 31 | switch action {
|
10 |
| - case .nullify: return "NULLIFY" |
| 32 | + case .noAction: return "NO ACTION" |
| 33 | + case .restrict: return "RESTRICT" |
| 34 | + case .cascade: return "CASCADE" |
| 35 | + case .setNull: return "SET NULL" |
| 36 | + case .setDefault: return "SET DEFAULT" |
11 | 37 | }
|
12 | 38 | }
|
13 | 39 | }
|
0 commit comments