Skip to Content
This documentation is still under construction
MethodsAlter tableForeign keys

Foreign keys

Foreign keys are changed from inside altertable with three keys: add_foreign_key, modify_foreign_key and drop_foreign_key.

Add foreign key

add_foreign_key takes an array of constraints. name, from, references and to are required; on_delete is optional.

{ "altertable": { "orders": { "add_foreign_key": [ { "name": "orders_account_fk", "from": "account_id", "references": "accounts", "to": "id" } ] } } }

Modify foreign key

modify_foreign_key has the same shape as add_foreign_key. Because a constraint cannot be changed in place, it is compiled to a drop of the existing constraint (by name) followed by adding it again with the new definition.

{ "altertable": { "orders": { "modify_foreign_key": [ { "name": "orders_account_fk", "from": "account_id", "references": "accounts", "to": "id", "on_delete": "cascade" } ] } } }

Drop foreign key

drop_foreign_key takes an array of constraint names.

{ "altertable": { "orders": { "drop_foreign_key": ["orders_account_fk"] } } }
Last updated on