Repository navigation
Conversation
d3vyce
force-pushed
the
fix/o2o-primary-key
branch
from
October 6, 2026 21:01
85d2908 to
39ac9e9
Compare
d3vyce
force-pushed
the
fix/o2o-primary-key
branch
from
October 7, 2026 16:01
39ac9e9 to
d577f5e
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes three bugs with a model whose primary key is a
OneToOneField. Both the relation and its<field>_idbacking field are flagged pk, and these code paths took the relation, which has no column:A ForeignKeyField to such a model now targets the backing field. Relations are set up in model name order, so when Post comes first, Profile.user is now initialized first, with or without to_field="user_id". That is also what makemigrations now writes: it wrote to_field='user', and the migrated table had no column either. Table names are now assigned before any relation, as Profile.user names its reverse relation after its table.
add_field() registers the pk filters for the backing field, which _init_relations adds after the class is created, and _pk_in_subquery() (#2269) only selects the pk fields that have a column.
Motivation and Context
I found no issue for these; related to #1771 / #1783 (many-to-many on such a model). A one-to-one primary key extends a table row for row, and other tables reference the extension: their foreign key silently stored nothing.
How Has This Been Tested?
New tests in
tests/test_relations.py(foreign keys from models set up before and after the target,pkfilters) andtests/test_queryset.py(delete and update through a related field), run on SQLite and PostgreSQL 18 (asyncpg). The full suite passes on SQLite and asyncpg. MySQL and MSSQL are left to CI.Checklist:
This fix has been partially implemented by Claude Opus 5.5. All modifications made by the AI have been reviewed by me.