Skip to content
Rhett Sutphin edited this page Mar 2, 2011 · 2 revisions

Foreign key support

Starting with version 0.7, Bering supports the creation of foreign key constraints when creating a table or adding a column to an existing table.

Syntax

When creating a table, pass the references named parameter to the addColumn method:

  createTable("mouse_cages") { t ->
    t.addColumn("mouse_id", "integer", references: "mice")
  }

When adding a column to an existing table, do the same thing:

  addColumn("observations", "observation_type_id", "integer", references: "observation_types")

The value passed to the references parameter should be the name of the table the foreign key constraint should reference.

Constraint names

In general, the generated constraints will be named fk_containingTableName_referencedTableName. In the Oracle dialect, the two table names will be truncated to 13 characters each so that the entire constraint will fit into Oracle's archaic 30 character identifier name limit.

If the generated name is not appropriate (e.g., if the truncated name collides with another generated name or you have multiple FKs from one table to another), you can override it using the referenceName parameter:

  addColumn("observations", "observation_type_id", "integer", references: "observation_types", referenceName: "fk_obs_obs_type")
Clone this wiki locally