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
idcolumn. - The referenced table must exist in the same project before you create the referencing table. A table can also reference itself, for example
parent_idon acategoriestable. - 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_tableor the web app’s New table dialog. They can’t be added to an existing table. - A
NULLvalue is always allowed in a nullable foreign key column. Setnullable: falseto 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
Createcompanies, then contacts with a foreign key that deletes contacts along with their company:
create_table arguments
id:
insert_rows arguments
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.