Fix reverse OneToOneField relations not being persisted to the database - #592
Fix reverse OneToOneField relations not being persisted to the database#592benaduo wants to merge 3 commits into
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
📝 WalkthroughWalkthroughAdds detection and persistence for reverse ChangesReverse one-to-one persistence
Estimated code review effort: 2 (Simple) | ~10 minutes Sequence Diagram(s)sequenceDiagram
participant Caller as baker.make()
participant Baker as Baker.instance()
participant Main as Created instance
participant Related as Related object
participant DB as Database
Caller->>Baker: provide reverse one-to-one keyword argument
Baker->>Baker: classify reverse descriptor
Baker->>Main: commit created instance
Baker->>Related: set forward relation to Main
Related->>DB: save using selected database
DB-->>Caller: return persisted relation
Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
2872d8a to
88f408a
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@model_bakery/baker.py`:
- Around line 722-737: The reverse OneToOne handler ( _handle_reverse_one_to_one
), as well as _handle_m2m and _handle_one_to_many, are saving/adding related
objects without honoring the baker DB alias; update each handler to propagate
self._using (following the pattern in _save_related_objs) by replacing plain
value.save() / related_obj.save() calls with value.save(using=self._using) (or
related_obj.save(using=self._using)) and ensure any creation/add operations in
_handle_m2m and _handle_one_to_many also use self._using (e.g., save the
through/related instances with using=self._using or pass the alias to creation
helpers) instead of relying on instance._state.db.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 1e309325-0937-40eb-af5a-172f31b2133f
📒 Files selected for processing (3)
CHANGELOG.mdmodel_bakery/baker.pytests/test_baker.py
|
Hi @amureki, quick note on this one:
Happy to make any changes. Thanks for your time. |
fcf7b6e to
526df55
Compare
|
Hi @amureki, this PR is ready for review. All CI checks are passing. Quick summary:
Happy to make any changes based on your feedback. |
amureki
left a comment
There was a problem hiding this comment.
Good contribution, thanks for diving into the complex topic!.
The change is looking good to me. And extra thanks for adding the tests! I left a couple of comments.
526df55 to
218e385
Compare
|
Hi @amureki, just a friendly bump on this one. I've rebased the branch against upstream/main to resolve the conflicts that accumulated over the past few months. All CI checks are passing. Would appreciate another look when you have time — happy to make any changes. Thanks! |
Hey hey! Do I miss something? My comments seem to be not addressed, plus CI is actually failing. Do I need to look into it or is it something you could still address? Thanks! |
|
Addressed the review comments:
|
Something is still odd - tests and linters are failing. |
Fixes model-bakers#473. When a reverse OneToOneField accessor was passed as a kwarg (e.g. baker.make(User, person=some_person)), Django's __init__ silently wrote it into the instance's __dict__ without setting the FK on the related object or saving it. Querying from the DB found no relation. Root cause: Baker.instance() only detected ForeignRelatedObjectsDescriptor (reverse FK). Reverse OneToOne uses ReverseOneToOneDescriptor, which was not handled. Fix: detect ReverseOneToOneDescriptor in instance(), extract those attrs, and handle them in a new _handle_reverse_one_to_one() method that sets the FK on the related object and saves it. # Conflicts: # CHANGELOG.md # model_bakery/baker.py
Address review: drop redundant commented-out lines from the regression test and reword the _handle_reverse_one_to_one docstring so it no longer restates the method name, and note consistency with _handle_one_to_many. Signed-off-by: Benjamin Aduo <manizza14@gmail.com>
The reverse OneToOne change made _classify_attrs return a four-tuple, but the existing TestClassifyAttrs tests still unpacked three values, so the full suite raised ValueError on every CI matrix job. Update the unpacking in the four tests to match the new signature and run black over the changed code. Signed-off-by: Benjamin Aduo <manizza14@gmail.com>
9ba69ae to
484b6a7
Compare
|
@amureki Following up on your Aug 10 note that tests and linters were still failing — I've since rebased the branch onto the latest upstream/main (Aug 16), and that fully resolved it. All checks are now green:
The two review points you raised are also addressed in the current head (commented-out lines removed from the regression test, docstring reworded). Would appreciate another look whenever you get a chance. Thanks! |
Fixes #473
When a reverse OneToOneField accessor was passed as a kwarg (e.g.
baker.make(User, person=some_person)), the relationship existed in memory but was never persisted — the FK on the related object was never set and never saved. Querying from the DB after the fact found nothing.Root cause:
Baker.instance()only detectedForeignRelatedObjectsDescriptor(reverse FK). Reverse OneToOne usesReverseOneToOneDescriptor, which was not handled. Django's__init__silently wrote it intoinstance.__dict__without ever updating the related object.Fix: detect
ReverseOneToOneDescriptorininstance(), extract those attrs, and handle them in a new_handle_reverse_one_to_one()method that sets the FK on the related object and saves it — consistent with how_handle_one_to_many()works for reverse FK relations.Changes
model_bakery/baker.py— newReverseOneToOneDescriptorimport; detection added toinstance()loop; new_handle_reverse_one_to_one()methodtests/test_baker.py— regression test inTestBakerCreatesAssociatedModelsverifying DB persistence viarefresh_from_db()CHANGELOG.md— entry under ChangedSummary by CodeRabbit
baker.make().CHANGELOG.mdunder Unreleased → Changed to note the fix.