Skip to content

ForeignKey

ForeignKey(to: Model, *, name: str = None, unique: bool = False, nullable: bool = True, related_name: str = None, virtual: bool = False, onupdate: Union[ReferentialAction, str] = None, ondelete: Union[ReferentialAction, str] = None, foreign_key_name: str = None, **kwargs: Any) has required parameters to that takes target Model class.

Sqlalchemy column and type are automatically taken from target Model.

  • Sqlalchemy column: class of a target Model primary key column
  • Type (used for pydantic): type of a target Model

Defining Models

To define a relation add ForeignKey field that points to related Model.

Python
1

Reverse Relation

ForeignKey fields are automatically registering reverse side of the relation.

By default it's child (source) Model name + s, like courses in snippet below:

Python
1

Reverse relation exposes API to manage related objects also from parent side.

Skipping reverse relation

If you are sure you don't want the reverse relation you can use skip_reverse=True flag of the ForeignKey.

If you set skip_reverse flag internally the field is still registered on the other side of the relationship so you can:

  • filter by related models fields from reverse model
  • order_by by related models fields from reverse model

But you cannot:

  • Access the related field from reverse model with related_name
  • Even if you select_related from reverse side of the model the returned models won't be populated in reversed instance (the join is not prevented so you still can filter and order_by over the relation)
  • The relation won't be populated in model_dump() and model_dump_json()
  • You cannot pass the nested related objects when populating from dictionary or json (also through fastapi). It will be either ignored or error will be raised depending on extra setting in pydantic Config.

Example:

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Author(ormar.Model):
    ormar_config = base_ormar_config.copy()

    id: int = ormar.Integer(primary_key=True)
    first_name: str = ormar.String(max_length=80)
    last_name: str = ormar.String(max_length=80)


class Post(ormar.Model):
    ormar_config = base_ormar_config.copy()

    id: int = ormar.Integer(primary_key=True)
    title: str = ormar.String(max_length=200)
    author: Optional[Author] = ormar.ForeignKey(Author, skip_reverse=True)

# create sample data
author = Author(first_name="Test", last_name="Author")
post = Post(title="Test Post", author=author)

assert post.author == author  # ok
assert author.posts  # Attribute error!

# but still can use in order_by
authors = (
    await Author.objects.select_related("posts").order_by("posts__title").all()
)
assert authors[0].first_name == "Test"

# note that posts are not populated for author even if explicitly
# included in select_related - note no posts in model_dump()
assert author.model_dump(exclude={"id"}) == {"first_name": "Test", "last_name": "Author"}

# still can filter through fields of related model
authors = await Author.objects.filter(posts__title="Test Post").all()
assert authors[0].first_name == "Test"
assert len(authors) == 1

add

Adding child model from parent side causes adding related model to currently loaded parent relation, as well as sets child's model foreign key value and updates the model.

Python
1
2
3
4
5
6
7
8
9
department = await Department(name="Science").save()
course = Course(name="Math", completed=False) # note - not saved

await department.courses.add(course)
assert course.pk is not None # child model was saved
# relation on child model is set and FK column saved in db
assert course.department == department
# relation on parent model is also set
assert department.courses[0] == course 

Warning

If you want to add child model on related model the primary key value for parent model has to exist in database.

Otherwise ormar will raise RelationshipInstanceError as it cannot set child's ForeignKey column value if parent model has no primary key value.

That means that in example above the department has to be saved before you can call department.courses.add().

Note

If the child already has a primary key value (i.e. a uuid pk with a default or an explicitly passed id on a non-autoincrement pk), ormar checks if the row exists in the database - if not the child is inserted, otherwise it's updated.

Warning

This method will not work on ManyToMany relations - there, both sides of the relation have to be saved before adding to relation.

remove

Removal of the related model one by one.

In reverse relation calling remove() does not remove the child model, but instead nulls it ForeignKey value.

Python
1
2
3
4
5
6
7
8
9
# continuing from above
await department.courses.remove(course)
assert len(department.courses) == 0
# course still exists and was saved in remove
assert course.pk is not None
assert course.department is None

# to remove child from db
await course.delete()

But if you want to clear the relation and delete the child at the same time you can issue:

Python
1
2
3
# this will not only clear the relation 
# but also delete related course from db
await department.courses.remove(course, keep_reversed=False)

clear

Removal of all related models in one call.

Like with remove, by default, clear() nulls the ForeigKey column on child model (all, not matter if they are loaded or not).

Python
1
2
# nulls department column on all courses related to this department
await department.courses.clear()

If you want to remove the children altogether from the database, set keep_reversed=False

Python
1
2
# deletes from db all courses related to this department 
await department.courses.clear(keep_reversed=False)

QuerysetProxy

Reverse relation exposes QuerysetProxy API that allows you to query related model like you would issue a normal Query.

To read which methods of QuerySet are available read below querysetproxy

You can overwrite related model field name by providing related_name parameter like below:

Python
1

Tip

The reverse relation on access returns list of wekref.proxy to avoid circular references.

Warning

When you provide multiple relations to the same model ormar can no longer auto generate the related_name for you. Therefore, in that situation you have to provide related_name for all but one (one can be default and generated) or all related fields.

Referential Actions

When an object referenced by a ForeignKey is changed (deleted or updated), ormar will set the SQL constraint specified by the ondelete and onupdate argument.

The possible values for ondelete and onupdate are found in ormar.ReferentialAction:

Note

Instead of ormar.ReferentialAction, you can directly pass string values to these two arguments, but this is not recommended because it will break the integrity.

CASCADE

Whenever rows in the parent (referenced) table are deleted (or updated), the respective rows of the child (referencing) table with a matching foreign key column will be deleted (or updated) as well. This is called a cascade delete (or update).

RESTRICT

A value cannot be updated or deleted when a row exists in a referencing or child table that references the value in the referenced table.

Similarly, a row cannot be deleted as long as there is a reference to it from a referencing or child table.

SET_NULL

Set the ForeignKey to None; this is only possible if nullable is True.

SET_DEFAULT

Set the ForeignKey to its default value; a server_default for the ForeignKey must be set.

Note

Note that the default value is not allowed and you must do this through server_default, which you can read about in this section.

DO_NOTHING

Take NO ACTION; NO ACTION and RESTRICT are very much alike. The main difference between NO ACTION and RESTRICT is that with NO ACTION the referential integrity check is done after trying to alter the table. RESTRICT does the check before trying to execute the UPDATE or DELETE statement. Both referential actions act the same if the referential integrity check fails: the UPDATE or DELETE statement will result in an error.

Overriding the foreign key constraint name

By default ormar generates the foreign key constraint name as fk_{source_table}_{target_table}_{target_pk}_{field_name}. On databases with a short identifier length limit (for example MySQL's 64 character limit) the auto-generated name can be truncated or rejected. Pass foreign_key_name to use a custom name instead:

Python
1
2
3
4
5
6
7
8
class Book(ormar.Model):
    ormar_config = base_ormar_config.copy(tablename="books")

    id: int = ormar.Integer(primary_key=True)
    author = ormar.ForeignKey(
        Author,
        foreign_key_name="fk_books_author",
    )

When used on an abstract base class, each subclass suffixes the name with its own tablename to avoid constraint name collisions across sibling tables.

Referencing a unique column (to_field)

By default a foreign key stores the related model's primary key. Legacy schemas often reference another unique column instead; pass its field name as to_field. The field must be the primary key, have unique=True, or be the only column of a UniqueColumns constraint.

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
class Country(ormar.Model):
    ormar_config = base_ormar_config.copy(tablename="countries")

    id: int = ormar.Integer(primary_key=True)
    iso_code: str = ormar.String(max_length=2, unique=True)


class Address(ormar.Model):
    ormar_config = base_ormar_config.copy(tablename="addresses")

    id: int = ormar.Integer(primary_key=True)
    country: Optional[Country] = ormar.ForeignKey(
        Country, to_field="iso_code", name="country_iso"
    )


await Address.objects.create(country="PL")  # by the iso_code value
address = await Address.objects.get()
address.country.iso_code  # "PL" - not loaded yet, pk unknown
await address.country.load()  # loads the country by iso_code

Relations without a database constraint (db_constraint)

db_constraint=False creates the column without a FOREIGN KEY constraint (for tables in another database, partitioned tables or schemas that don't declare their foreign keys). Joins, filters and prefetching work the same. ondelete/onupdate need a constraint, so they can't be combined with it.

Python
1
2
3
4
5
class Event(ormar.Model):
    ormar_config = base_ormar_config.copy(tablename="events")

    id: int = ormar.Integer(primary_key=True)
    user: Optional[User] = ormar.ForeignKey(User, db_constraint=False)

Foreign keys in the primary key

A foreign key can be (part of) the primary key: link tables keyed by their foreign keys, or a table sharing its primary key with another one. The key value holds the referenced values, not the related models.

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Enrollment(ormar.Model):
    ormar_config = base_ormar_config.copy(tablename="student_course")

    student: Student = ormar.ForeignKey(Student, primary_key=True, name="student_id")
    course: Course = ormar.ForeignKey(Course, primary_key=True, name="course_id")
    grade: Optional[int] = ormar.Integer(nullable=True)


enrollment = await Enrollment.objects.create(student=ann, course=math)
enrollment.pk  # (ann.id, math.id)
await Enrollment.objects.get(pk=(ann.id, math.id))
await Enrollment.objects.get(student=ann, course=math)  # the same row

Foreign keys in the primary key are not nullable. A foreign key stored in several columns can be part of the key too (see link tables keyed by foreign keys to several columns).

Foreign keys to several columns

A foreign key to a model with a composite primary key, or to a set of columns that is unique together (to_field=[...], matching a UniqueColumns constraint), is stored in one column per referenced column, with one composite FOREIGN KEY constraint. The columns are named <field>_<referenced column> unless columns= maps each referenced column (by its database name) to a column name. name= can't be used for them.

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Order(ormar.Model):
    ormar_config = base_ormar_config.copy(tablename="orders")

    tenant_id: int = ormar.Integer(primary_key=True)
    number: int = ormar.Integer(primary_key=True, name="order_no")


class OrderLine(ormar.Model):
    ormar_config = base_ormar_config.copy(tablename="order_lines")

    id: int = ormar.Integer(primary_key=True)
    order: Optional[Order] = ormar.ForeignKey(
        Order, columns={"tenant_id": "ord_tenant", "order_no": "ord_no"}
    )
    # FOREIGN KEY (ord_tenant, ord_no) REFERENCES orders (tenant_id, order_no)


await OrderLine.objects.create(order=order)  # a model,
await OrderLine.objects.create(order=(1, 7))  # its key tuple
await OrderLine.objects.create(order={"tenant_id": 1, "number": 7})  # or a dict

await OrderLine.objects.filter(order=order).all()
await OrderLine.objects.filter(order__in=[(1, 7), (1, 8)]).all()
await OrderLine.objects.filter(order__isnull=True).all()
await OrderLine.objects.filter(order__note="first").all()  # through the relation
await Order.objects.prefetch_related("lines").get(pk=(1, 7))

await OrderLine.objects.values(["id", "order"])
# [{"id": 1, "order": {"tenant_id": 1, "number": 7}}]
await OrderLine.objects.values_list(["id", "order"])  # [(1, (1, 7))]
line.model_dump(flatten_fields={"order"})["order"]  # {"tenant_id": 1, "number": 7}
  • Only =, __in and __isnull lookups work on the relation itself; filter other conditions through the related fields. A key with a missing part (filter(order={"tenant_id": 1})) raises QueryDefinitionError.
  • order_by("order") / order_by("-order") sorts by all its columns, in key order.
  • A row with any of the columns NULL references no row: the relation reads as None and matches order__isnull=True.
  • unique=True (and OneToOne) adds a unique constraint over all the columns, index=True one index over them.
  • A key can also be given as a list ([1, 7], as JSON has no tuples). In get_pydantic(flatten_fields=...) the relation is a typed dict of the referenced fields, validated per field.

A foreign key to several columns can be part of the primary key, and fields can share a column: name the same column in columns= of several foreign keys (or use the name of a plain field). The table creates the column once. A typical multi-tenant link table:

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class Member(ormar.Model):
    ormar_config = base_ormar_config.copy(tablename="members")

    tenant_id: int = ormar.Integer(primary_key=True)
    login: str = ormar.String(max_length=20, primary_key=True)


class Team(ormar.Model):
    ormar_config = base_ormar_config.copy(tablename="teams")

    tenant_id: int = ormar.Integer(primary_key=True)
    code: str = ormar.String(max_length=10, primary_key=True)


class Membership(ormar.Model):
    ormar_config = base_ormar_config.copy(tablename="memberships")

    tenant_id: int = ormar.Integer(primary_key=True)  # optional
    member: Member = ormar.ForeignKey(
        Member,
        primary_key=True,
        columns={"tenant_id": "tenant_id", "login": "member_login"},
    )
    team: Team = ormar.ForeignKey(
        Team,
        primary_key=True,
        columns={"tenant_id": "tenant_id", "code": "team_code"},
    )
    # PRIMARY KEY (tenant_id, member_login, team_code)
    # FOREIGN KEY (tenant_id, member_login) REFERENCES members
    # FOREIGN KEY (tenant_id, team_code) REFERENCES teams


membership = await Membership.objects.create(member=alice, team=core)
membership.pk  # (1, "alice", "core"): one part per key column
membership.tenant_id  # 1, filled in from the foreign keys
await Membership.objects.get(pk=(1, "alice", "core"))
await Membership.objects.get(member=alice, team=core)  # the same row
  • The key value has one part per key column, a shared column once, in the order the columns first appear.
  • A shared column gets the value its fields agree on. Two different values (a member and a team of different tenants) raise ModelPersistenceError on save(), update(), bulk_create(), bulk_update() and queryset update(). A queryset update() checks only the values it is given, and the database's foreign keys check the rest.
  • Setting a foreign key that shares a column to None writes NULL to its own columns and leaves the shared column as it is. The relation then reads as None. The shared column becomes NULL only when every foreign key storing it is None and no plain field stores it.
  • A plain field sharing the column that isn't set is filled in from the foreign keys when the model is created (Membership(member=alice, team=core).tenant_id == 1, so model_dump() and pk agree before saving), which also lets a required plain field be left out. A foreign key assigned later fills the plain field on save().
  • exclude_fields() / fields() work per field: excluding the plain field leaves it None on loaded models (even a required one) while its foreign keys still read the column, and excluding a foreign key leaves the plain field loaded.
  • update(_columns=[...]) writes only the listed columns: updating the shared column alone leaves the other columns of its foreign keys as they are, so the database's foreign keys reject a pair that no longer exists.
  • With partial updates, assigning a foreign key writes all of its columns, the shared one included, so a value that conflicts with the other fields still raises ModelPersistenceError.
  • Only columns named explicitly (columns= / name=) are shared. A field whose name clashes with a generated column name (<field>_<column>) still raises ModelDefinitionError, as do two plain fields on one column and fields giving a shared column different types (String(3) and String(20), or BigInteger and Integer, differ too).
  • The shared column keeps the options of every field storing it on its own: the foreign key constraint of a foreign key to one column (name="tenant_id"), and the index, unique, default, server_default and comment of a plain field, whichever field is declared first. It is part of the primary key when any of its fields is, and nullable only when all of them are.
  • A link table like this can be the through model of a ManyToMany (see many to many).

A model whose primary key holds foreign keys (like Membership above) is referenced like any other model with a composite key: a ForeignKey, OneToOne or ManyToMany to it stores one column per key column, each shared column once, and a link table can declare a ManyToMany itself.

Python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Shift(ormar.Model):
    ormar_config = base_ormar_config.copy(tablename="shifts")

    id: int = ormar.Integer(primary_key=True)
    membership: Membership = ormar.ForeignKey(Membership)
    # membership_tenant_id, membership_member_login, membership_team_code
    # FOREIGN KEY (...) REFERENCES memberships (tenant_id, member_login, team_code)


class Seniority(ormar.Model):  # keyed by the link table's key
    ormar_config = base_ormar_config.copy(tablename="seniorities")

    membership: Membership = ormar.OneToOne(Membership, primary_key=True)
    years: int = ormar.Integer(default=0)


await Shift.objects.create(membership=membership)
await Shift.objects.create(membership=(1, "alice", "core"))  # key tuple
await Shift.objects.filter(membership__member__login="alice").all()
shift.model_dump(flatten_fields={"membership"})["membership"]
# {"tenant_id": 1, "member": {"tenant_id": 1, "login": "alice"},
#  "team": {"tenant_id": 1, "code": "core"}}
  • columns= is keyed by the columns of the referenced key ({"tenant_id": ..., "member_login": ..., "team_code": ...}), and the default column names are <field>_<key column>.
  • A key value is the flat tuple of the key columns (membership.pk). A dict of the key fields works too, with a foreign key in the key given as a model, a dict of its fields or its own key tuple.
  • Flattened output (flatten_fields, get_pydantic(flatten_fields=...)) and values() give the dict of the key fields, where a foreign key in the key is represented by its own flattened value (nested dicts above). values_list() gives the flat tuple.

Relation Setup

You have several ways to set-up a relationship connection.

Model instance

The most obvious one is to pass a related Model instance to the constructor.

Python
1

Primary key value

You can setup the relation also with just the pk column value of the related model.

Python
1

Dictionary

Next option is with a dictionary of key-values of the related model.

You can build the dictionary yourself or get it from existing model with model_dump() method.

Python
1

None

Finally you can explicitly set it to None (default behavior if no value passed).

Python
1

You can also clear a ForeignKey relation on an already-loaded model by assigning None — the field must be nullable for this to persist. The related model is unregistered from the instance immediately, so subsequent reads return None without requiring a reload:

Python
1
2
3
4
5
6
7
track = await Track.objects.select_related("album").get(id=track_id)
assert track.album is not None

track.album = None
assert track.album is None  # in-memory state cleared

await track.update(_columns=["album"])  # persists NULL to the database

Note

Assigning None to a non-nullable ForeignKey is ignored at the in-memory level. Make the field nullable=True (the default) if you need to unassign it.

Warning

In all not None cases the primary key value for related model has to exist in database.

Otherwise an IntegrityError will be raised by your database driver library.