Foreign Keys

A foreign key is a column, or a set of columns, that points to a row in another table. A foreign key can prevent you from adding an order for a customer who doesn't exist. In DbSchema, you create a foreign key by dragging one column onto another, and DbSchema draws it as a line between the two tables.

The tables countries, addresses, customers, payments, orders and order_items on a DbSchema diagram, with foreign key lines between them

In the picture, orders.customer_id identifies the customer who placed the order. The table that contains the foreign key is called the child table, here orders. The table that it references is called the parent table, here customers.

The design, also called the model, is your DbSchema project: the tables, columns and diagrams that you see in DbSchema. The database is the real database on the server. Each task below says which of the two it changes. Database Terms explains foreign key, cardinality and the other words on this page.

Create a foreign key

The parent column must be a primary key or have a unique key. Otherwise, the database refuses the foreign key.

Drag one column onto another to open the foreign key dialog with the columns already filled in.

  1. Drag the child column, such as order_id in shipments, onto the parent column, such as order_id in orders.
  2. The New Foreign Key dialog opens with the fields already filled in. Check the name and the columns.
  3. Press OK, or Enter.

The new line joins the two tables. If you are connected, DbSchema creates the foreign key in the database at once. If you work offline, the foreign key stays in the design until you synchronize the design with the database. Synchronization needs DbSchema Pro.

A foreign key to customers.last_name, which has no unique key, fails in PostgreSQL 17 with:

ERROR:  there is no unique constraint matching given keys for referenced table "customers"

Edit a foreign key

Double-click the line to open the Edit Foreign Key dialog.

The Edit Foreign Key dialog of DbSchema for orders_customer_id_fkey, with customer_id as the referring and the referred column, and On Delete set to noAction
FieldWhat it sets
NameThe name of the constraint in the database
VirtualWhether the foreign key stays in the design only
Schema, TableThe parent table
Referring Column, Referred ColumnOne pair per column of the foreign key
On Delete, On UpdateWhat the database does with the child rows
TypeThe cardinality that the line shows
Deferrable...When PostgreSQL checks the foreign key, such as DEFERRABLE INITIALLY DEFERRED for the end of the transaction

If a foreign key uses several columns, the dialog shows one row for each pair of columns, with Referring Column and Referred Column. Add adds a row. On Delete controls what happens to the child rows when you delete the parent row. On Update controls what happens when you change the parent's key value. Read the cardinality of a line explains Type, and Add a virtual foreign key explains Virtual.

The Optimizations tab has options that apply when you create a foreign key:

  • Ensure Primary Key Index adds a unique key on the parent columns when they have none.
  • Optimize deletions by indexing the referring column(s) adds an index on the child columns.

On Delete and On Update

ActionWhat the database does when the parent row is deleted
noActionRefuses the delete while child rows exist
restrictRefuses the delete, like noAction
cascadeDeletes the child rows too
setNullSets the child column to NULL

With On Update, cascade copies the changed key to the child rows. setNull works only on a child column that allows NULL. The list offers only the actions that your database supports.

With noAction or restrict, delete the child rows first. PostgreSQL 17 refuses to delete an order if a payment still references it:

ERROR:  update or delete on table "orders" violates foreign key constraint "payments_order_id_fkey" on table "payments"
DETAIL:  Key (order_id)=(1) is still referenced from table "payments".

Read the cardinality of a line

Cardinality says how many rows on one side match a row on the other side. For example, one customer can have many orders. DbSchema draws the cardinality with marks at the two ends of each line.

A database has no setting for cardinality. DbSchema determines it from the settings of the child columns:

Child columnsUniqueLine ends, parent to child
Mandatory ( Not Null )No1 to 1 or many
Mandatory ( Not Null )Yes1 to 1
NullableNo0 or 1 to 0 or many
NullableYes0 or 1 to 0 or 1

The child columns are unique when a primary key, a unique key or a unique index uses exactly the same columns as the foreign key. Nullable means that the column allows NULL, which marks a missing value. In every notation except Idef1x, DbSchema draws a dashed line when the child column is nullable.

The pictures below show three foreign keys to orders. Their child columns are defined like this:

CREATE TABLE order_items (
    order_id   integer NOT NULL REFERENCES orders,
    product_id integer NOT NULL,
    PRIMARY KEY (order_id, product_id)
);

CREATE TABLE payments (
    payment_id integer PRIMARY KEY,
    order_id   integer REFERENCES orders
);

CREATE TABLE shipments (
    shipment_id integer PRIMARY KEY,
    order_id    integer NOT NULL UNIQUE REFERENCES orders
);

In the Information Engineering notation, DbSchema marks both ends of each line:

Three lines to orders on a DbSchema diagram in Information Engineering notation: from order_items with a bar and a crow's foot, from payments with a circle and a crow's foot, and from shipments with a single bar at each end
  • order_items to orders: order_id is mandatory. It is one of the two columns of the primary key, so it isn't unique on its own. A bar and a crow's foot at the child end mean one or many.
  • payments to orders: order_id is nullable. A circle and a crow's foot at the child end mean zero or many. The circle and the bar at the parent end mean that a payment may have no order.
  • shipments to orders: order_id is mandatory and unique. A single bar at each end means one to one.

The Type line of the foreign key dialog describes the same cardinality in text, such as Type 1:[1 or many] or Type 1:1.

The marks show what the columns allow. The database doesn't check that a parent row has child rows. For example, the database accepts an order without any order_items row, although the line from order_items shows one or many.

Change the notation

Choose how DbSchema draws the line ends in View → Foreign Keys → Foreign Key Notation:

NotationHow it marks the line
Information Engineering With ArrowsThe default: the marks above at the child end, an arrow at the parent end
Information EngineeringMarks at both ends, as in the picture above
BarkerA crow's foot for many, a dashed half of the line for optional
Idef1xA filled dot at the child end, with a letter for the cardinality
The same three lines to orders on a DbSchema diagram in Information Engineering With Arrows notation, each with an arrow at the orders end

The notation changes only the diagram. The design and the database stay the same.

Change the cardinality

To change the cardinality, change the child column. That changes the table itself. If you are connected, the change runs in the database.

  • Zero instead of one: clear Mandatory ( Not Null ) in the column dialog, so that the column accepts NULL.
  • One instead of many: add a unique key on the child column in the Primary Key / Unique / Indexes tab of the table, as Create an index shows.

To display zero or many without changing the column, tick View → Foreign Keys → Foreign Key Notation → Use Strict Conventions. This changes only the diagram. DbSchema then draws the marks at the child end only from whether the child columns are unique. If the child column isn't unique, the line shows zero or many. If it is unique, the line shows zero or one.

The three lines to orders on a DbSchema diagram with Use Strict Conventions ticked: order_items now shows a circle and a crow's foot, and shipments a circle and a bar

To set the cardinality yourself, without changing the columns, use logical design. Logical design needs DbSchema Architect. There, the foreign key dialog offers Optional or Mandatory, and Zero or More, One or More, Zero or One or One.

Add a virtual foreign key

A virtual foreign key exists only in the DbSchema design. DbSchema never creates it in the database. Use one when two tables hold related data but have no foreign key in the database.

  1. Drag the child column onto the parent column, as in Create a foreign key.
  2. Tick Virtual in the dialog.
  3. Press OK.
The New Foreign Key dialog of DbSchema for fk_shipments_orders, from shipments.order_id to orders.order_id, with Virtual ticked

DbSchema draws a virtual foreign key in another color:

A teal virtual foreign key line from shipments to orders on a DbSchema diagram, beside the gray line of the real foreign key from payments

To keep a virtual foreign key, save the design file. Saving needs DbSchema Pro. The Query Builder and the Data Explorer treat virtual foreign keys like database foreign keys. Both tools need DbSchema Pro. Model → Find Virtual Foreign Keys suggests virtual foreign keys between columns with matching names.

While you are connected, the Virtual box is disabled on a foreign key that exists in the database. You can't make such a foreign key virtual.

Common problems

The cardinality is stuck at 1:1

A primary key or a unique key uses exactly the foreign key columns. That makes the child columns unique. This often happens when the foreign key column is also the whole primary key of the child table. To change the relationship to one to many, give the child table a primary key of its own. Then drop the unique key on the foreign key columns.

All my lines show 1:[1 or many], but I need 0..n

The child columns are Mandatory ( Not Null ), which DbSchema reads as "at least one". Tick Use Strict Conventions to show zero or many without changing the columns, as Change the cardinality shows.

How do I make the parent side optional?

In the Information Engineering notation, the parent end shows a circle when the child column is nullable. The default notation always draws an arrow there. To set it yourself, use the Optional and Mandatory choices of logical design.

Why is my line dashed?

The child column is nullable. To make the line solid, tick Mandatory ( Not Null ) in the column dialog. This changes the table, and the database too if you are connected. A deduced foreign key is always dashed. View → Diagram Display → Deduced Foreign Keys shows these lines.

Next steps