Foreign keys

Last modified: 27 August 2021

Professional feature: download PyCharm Professional to try.

Foreign key relationships specify how tables relate to each other and indicate relationships between tables. PyCharm recognizes foreign key relationships in your database schema and uses them to construct JOIN clauses. You can see these relationships in the auto-completion list, data navigation, and diagrams.

In PyCharm, you can use explicit foreign keys or create virtual foreign keys.

A foreign key is a field or a collection of fields in one table that refers to the primary key in another table. When you create or modify a table, you can clearly define those keys:

The table that contains a foreign key is a child table. The table that contains a candidate key is a referenced or target table. If your database contains explicit foreign key relationships, PyCharm automatically uses them in auto-completion, data navigation, and diagrams.

In the following example, activity.activity_id is a primary key, while visitor.activity_id is a foreign key.

Explicit foreign keys