Skip to content

Guide: pull request #21173, ForeignObject and on_delete

Pull request #21173, "Fixed #34122 -- Added reference docs for ForeignObject.", by 1wos, adds the first reference entry for ForeignObject to Django's model field docs (ticket #34122). It is a documentation-only change, opened on 25 April 2026, with one round of review comments addressed on 30 April. On 12 November 2026 another user asked for it to be merged. It is on my ORM watchlist.

This guide was written on 12 November 2026, with the help of Claude. Claude ran the tests below on Django's main branch. My task is to understand them well enough to run them myself, explain every result, and decide what, if anything, to say on the pull request. I have not commented on it yet.

The task

By the end of this guide I can:

(i) explain what a ForeignObject is, and why it creates no column, no constraint and no index;

(ii) run the test script myself and say, for each line of output, which part of Django's code produces it;

(iii) say which sentences of the new docs match what Django does, and which do not;

(iv) choose my next step: a comment on the pull request, a new ticket, or a question for my Navigator.

What was tested, 12 November 2026

Setup. Django main at commit a0f2e29e0a (12 November 2026, version 6.2 in development), SQLite in memory, Python 3. Tables 1, 2 and 4 gave the same results on my local clone of 3 September 2026. The script is foreignobject_check.py. Each test makes its own parent model, with unique_together on two fields a and b, and a child model whose ForeignObject joins (pa, pb) to (a, b).

1. Deleting the parent, for each on_delete option

The pull request says: "ForeignObject.on_delete: Behaves the same as ForeignKey.on_delete."

on_delete What happens when the parent is deleted Same as ForeignKey?
CASCADE The child is deleted Yes
PROTECT ProtectedError, nothing deleted Yes
RESTRICT RestrictedError, nothing deleted Yes
DO_NOTHING The child stays, still pointing at the deleted parent Partly: with a ForeignKey, the database's constraint would normally refuse the orphan (not tested here)
SET_NULL FieldError: Cannot update model field <ForeignObject: p> (only concrete fields are permitted) No
SET_DEFAULT The same FieldError No
SET(0) The same FieldError No
DB_CASCADE The child stays No, nothing happens
DB_SET_NULL The child stays, (pa, pb) still (1, 2) No, nothing happens
DB_SET_DEFAULT The child stays, (pa, pb) still (1, 2) No, nothing happens

Two different causes. (i) The SET_* options ask the ORM to update the ForeignObject field to NULL or a default. A ForeignObject has no column of its own, so the update is refused (add_update_values in django/db/models/sql/subqueries.py). The columns that would have to change are pa and pb. (ii) The DB_* options, new in Django 6.1, leave the work to the database's ON DELETE clause. The collector skips them (SKIP_COLLECTION in django/db/models/deletion.py), and a ForeignObject creates no foreign key constraint, so nobody does the work.

2. System checks for on_delete

Model Check result
ForeignKey, SET_NULL, field not nullable fields.E320
ForeignObject, SET_NULL, fields not nullable No error

The on_delete checks (fields.E320 to fields.E324) are in ForeignKey._check_on_delete, in django/db/models/fields/related.py. ForeignObject does not run them, so the mistakes in table 1 pass check silently.

3. models.E049, under from_fields

The pull request says: "When from_fields contains more than one entry, it cannot appear in indexes, constraints, or unique_together. Doing so raises models.E049."

What is named in Meta Check result
Index(fields=["p"]), the ForeignObject itself models.E049
unique_together = [("p", "pa")] models.E049
Index(fields=["pa", "pb"]), the fields listed in from_fields No error

So "it" is the ForeignObject field, not from_fields. The fields in from_fields can be indexed.

4. to_fields checks

The pull request says the target columns must be unique, "Otherwise the system check framework raises fields.E310, fields.E311, or fields.E312."

Target model Check result
No uniqueness on (a, b) fields.E310
UniqueConstraint on (a, b) with a condition fields.E310
UniqueConstraint on (a, b) without a condition No error
to_fields names a field that does not exist on the target fields.E312

So fields.E312 is not about uniqueness, and a conditional UniqueConstraint does not count.

Not tested

PostgreSQL or any database other than SQLite. fields.E311 (a single field in to_fields). The documentation build. Whether a ticket for the SET_NULL error already exists: Django's tracker cannot be read automatically, so that is step 5 below. One trap found on the way: the checks on Meta.indexes and Meta.constraints only run when check() is given databases=, which is why the first run showed no models.E049 for the index.

Learning path

Six sessions of about an hour, from 14 November, before the contribute phase starts on 28 November. Each one ends on one output. The tutorials were done on 7 November, so the time planned for them on 14–15 November is free. Sessions 1 and 2 also prepare the Models topic guide of 21–22 November.

Session Steps Output
1 Step 0 on_delete in my own words, Python-level and database-level
2 Step 1 ForeignObject in my own words, with the example from the docs
3 Step 2 The script run on my laptop, results the same as above
4 Step 3 The path from delete() to the FieldError, written down
5 Step 4 Each check code linked to the function that raises it
6 Steps 5 and 6 My decision, and the question for Thursday's check-in

Step 0: on_delete on a ForeignKey first

  • Read ForeignKey.on_delete in the model field reference, all the options.
  • Read the Django 6.1 release note "Database-level delete options for ForeignKey.on_delete" (docs/releases/6.1.txt).

Question to answer in your own words: who does the work for CASCADE, and who does it for DB_CASCADE? What has to exist in the database for DB_CASCADE to work?

Hint: one of them loads the related objects in Python before deleting them, the other writes ON DELETE CASCADE into the table definition.

Step 1: what a ForeignObject is

  • Read the topic guide Composite primary keys, the part on relations: why a ForeignKey cannot point to a model with a CompositePrimaryKey.
  • Read the new section in the pull request: the Files changed tab, docs/ref/models/fields.txt.
  • Open django/db/models/fields/related.py and find class ForeignObject and class ForeignKey. Which one inherits from the other?

Questions: (i) a ForeignKey named parent creates a column parent_id. What does a ForeignObject named p create? (ii) If you wanted Django to set the relation to NULL, which columns would actually have to change?

Step 2: run the tests yourself

  • Make a working folder with an empty app, and copy foreignobject_check.py into it:

    mkdir -p ~/fo-test/app
    touch ~/fo-test/app/__init__.py ~/fo-test/app/models.py
    cd ~/fo-test
    python3 -m venv .venv && source .venv/bin/activate
    
  • Install Django from your clone on an up-to-date main branch, not your practice branch:

    cd ~/path/to/your/django/clone
    git remote add upstream https://github.com/django/django.git   # once
    git fetch upstream
    git switch -c fo-test upstream/main
    pip install -e .
    cd ~/fo-test
    
  • Before you run it, write down what you expect for each of the ten on_delete options in table 1.

  • Run python foreignobject_check.py and compare with your guesses and with the tables above.
  • Change one thing and run it again: remove databases=["default"] from check_ids(). Which result disappears, and why?

Hint: if import app fails, you are not in ~/fo-test, or app/__init__.py is missing.

Step 3: follow the FieldError

  • In django/db/models/deletion.py, read the functions SET_NULL, SET_DEFAULT and SET at the top of the file. Each one calls collector.add_field_update(...). With which field?
  • Find SKIP_COLLECTION and read where it is used in Collector.collect.
  • In django/db/models/sql/subqueries.py, find the message "only concrete fields are permitted". What makes a field concrete?
  • Optional: put breakpoint() just before row.delete() in the script and step into it with s and n, as in "Debugging at Django" (week 5 of the programme).

Question: why do CASCADE, PROTECT and RESTRICT work, when the three SET_* options do not?

Hint: the first three never update the child row.

  • In related.py, find ForeignKey._check_on_delete. Then read ForeignObject.check. Is _check_on_delete in its list?
  • In the same file, find _check_to_fields_exist (fields.E312) and _check_unique_target (fields.E310, fields.E311). Where does it skip a UniqueConstraint with a condition? (Hint: total_unique_constraints in django/db/models/options.py.)
  • In django/db/models/base.py, find models.E049 in _check_local_fields. Which variable is the field being tested, and is it the ForeignObject or a field from from_fields?
  • Read the entries for these codes in docs/ref/checks.txt.

Question: rewrite the from_fields sentence of the pull request so that "it" cannot be misread.

Step 5: look before you write

Step 6: decide

Three possible next steps, not exclusive:

(i) A comment on pull request #21173: the on_delete sentence, with the reproduction. This is a documentation question, and it fits in the pull request.

(ii) A new ticket for the SET_NULL error, if none exists: a ForeignObject with SET_NULL, SET_DEFAULT or SET() fails on delete, and passes check. That is a question about code, not docs, and the two should not be mixed in one pull request.

(iii) A question for my Navigator at Thursday's check-in, before either.

  • Choose, and write down why.
  • Whatever I post, I say what an AI tool was used for.

Is a documented limitation enough here, or should ForeignObject refuse SET_NULL and the DB_* options with a system check?