Module Table.Foreign_key

Foreign keys definitions.

Foreign keys represent SQL FOREIGN KEY constraints on a table.

We use the parent-child terminology of SQLite. The parent table is the table that the foreign key refers to. The child table is the table making the reference.

type action = [
  1. | `Set_null
    (*

    The child key becomes NULL.

    *)
  2. | `Set_default
    (*

    The child key is set to the default.

    *)
  3. | `Cascade
    (*

    The row with the child key gets deleted or updated.

    *)
  4. | `Restrict
    (*

    The parent key cannot be deleted or updated.

    *)
]

The type for foreign key actions on parent delete or updates.

type parent =
  1. | Table : 's t * 's Col.def list -> parent
  2. | Self : 's Col.def list -> parent

This is the parent table and the columns that are referenced within. Use Self to refer to table being defined.

Note. For now the module does not check that the list of columns actually belong the parent table. Do not rely on this Invalid_argument may be raised in the future on make.

type 'r t

The type for foreign keys.

val make : ?on_delete:action -> ?on_update:action -> cols:'r Col.def list -> parent:parent -> unit -> 'r t

v ?on_delete ?on_update ~cols ~parent () is a foreign key with given arguments. See accessors for semantics.

val cols : 'r t -> 'r Col.def list

cols fk are the child table columns of fk.

val parent : 'r t -> parent

parent fk is the parent table of fk.

val on_delete : 'r t -> action option

on_delete fk is the action taken whenever the parent key of fk is deleted.

val on_update : 'r t -> action option

on_update fk is the action taken whenever the parent key of fk is updated.