Skip to main content
A foreign key makes a uuid column point at a row in another table. Postgres then guarantees that every non-null value in the column matches the id of an existing row.

Rules

  • The column must have type uuid.
  • It always references the other table’s id column.
  • The referenced table must exist in the same project before you create the referencing table. A table can also reference itself, for example parent_id on a categories table.
  • Each column can have at most one foreign key. A table can have several foreign keys on different columns.
  • Foreign keys are defined when the table is created, with create_table or the web app’s New table dialog. They can’t be added to an existing table.
  • A NULL value is always allowed in a nullable foreign key column. Set nullable: false to require a reference.

Delete rules

onDelete decides what happens to referencing rows when a referenced row is deleted:
Use set null only on nullable columns. On a NOT NULL column, deleting a referenced row fails. The web app’s New table dialog always uses restrict. Ask Claude, or use create_table directly, for cascade or set null.

Example

Create companies, then contacts with a foreign key that deletes contacts along with their company:
create_table arguments
To insert a contact, use a company’s id:
insert_rows arguments
An id that doesn’t exist in companies fails with:

Dropping linked tables and columns

  • Dropping a column also removes its foreign key.
  • A table can’t be dropped while another table references it. Drop the referencing column or table first.

Viewing relationships

The project’s Schema tab draws a line between each foreign key column and the table it references. list_tables and describe_table return foreign keys in foreignKeys.