Skip to content

Add regression test for bulk_create with M2M field using explicit related_name#582

Merged
amureki merged 1 commit intomodel-bakers:mainfrom
benaduo:fix/bulk-create-m2m-related-name-test
Mar 20, 2026
Merged

Add regression test for bulk_create with M2M field using explicit related_name#582
amureki merged 1 commit intomodel-bakers:mainfrom
benaduo:fix/bulk-create-m2m-related-name-test

Conversation

@benaduo
Copy link
Contributor

@benaduo benaduo commented Mar 10, 2026

Summary

  • Adds a regression test to TestCreateM2MWhenBulkCreate covering the scenario
    reported in baker.make() with _bulk_create=True fails for M2M-fields that have a reverse_name #489: calling baker.make() with _bulk_create=True on a model
    whose ManyToManyField defines an explicit related_name
  • Uses Store.customers (related_name="favorite_stores") with _quantity=10,
    asserting that all bulk-created instances have the correct M2M rows populated

Motivation

Issue #489 reported a TypeError where the through-table constructor was being
called with the related_name as a keyword argument
(Store_customers(favorite_stores=...)). That bug was fixed in a prior release,
but no regression test was ever added for this specific case, leaving the fix
unguarded against future regressions.

Changes

  • tests/test_baker.py: add test_create_with_field_with_related_name to
    TestCreateM2MWhenBulkCreate

Testing

pytest tests/test_baker.py::TestCreateM2MWhenBulkCreate -v

Author

Benjamin Aduo (@benaduo)

Summary by CodeRabbit

  • Tests
    • Added coverage verifying bulk-create behavior for many-to-many relationships, ensuring related entries are correctly associated when populated via a field with an explicit related name.

@benaduo
Copy link
Contributor Author

benaduo commented Mar 10, 2026

The ty linter failures in this CI run are unrelated to the changes in this PR.

All 13 errors flagged by ty check model_bakery are in pre-existing code:

  • baker.py:623other_fields_to_skip.extend([GenericRelation, GenericForeignKey])
  • baker.py:707values = values()
  • generators.py:119–137 — conditional field mappings (ArrayField, HStoreField, CICharField, etc.)
  • generators.py:150 — return type of get_type_mapping()

None of these lines were modified by this PR. The same errors appear on PR #583 and would affect any PR raised today. Cross-referencing against the last successfully merged PR (#576, Feb 19), the ty check was passing at that point — this appears to be a regression introduced by a ty version upgrade in the CI environment since then (the CI downloads ty fresh on each run without a pinned version).

Happy to address these in a separate PR if that would be helpful, but they are out of scope for this change.

@benaduo benaduo force-pushed the fix/bulk-create-m2m-related-name-test branch from 6cc8624 to d7cfd19 Compare March 19, 2026 15:24
@coderabbitai
Copy link

coderabbitai bot commented Mar 19, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 62214833-a356-4be5-9bbd-f33d1303ec2d

📥 Commits

Reviewing files that changed from the base of the PR and between d7cfd19 and ccd776c.

📒 Files selected for processing (1)
  • tests/test_baker.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/test_baker.py

📝 Walkthrough

Walkthrough

Added a new Django DB test test_create_with_field_with_related_name that bulk-creates Store instances with a customers many-to-many field (using _bulk_create=True and _quantity=10) and asserts the M2M through-table correctly links each created Store to the original Person.

Changes

Cohort / File(s) Summary
M2M Bulk Create Test
tests/test_baker.py
Add TestCreateM2MWhenBulkCreate::test_create_with_field_with_related_name to verify M2M population for fields with an explicit related_name when using _bulk_create and _quantity.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Possibly related PRs

Poem

🐰 I hopped into tests with a joyful cheer,
Bulk-made ten stores with one friend near.
Through-table bridges now snug and tight,
Customers and stores connected right.
Hooray for green tests in the moonlight! 🥕

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: adding a regression test for bulk_create with M2M fields that have explicit related_name.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
📝 Coding Plan
  • Generate coding plan for human review comments

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 `@tests/test_baker.py`:
- Around line 1273-1274: The test only samples two stores (s1, s2) but should
verify every bulk-created Store row; replace the sliced selection that uses
models.Store.objects.all()[:2] with iterating over all stores
(models.Store.objects.all()) and assert for each Store instance (e.g., variable
name like store) that list(store.customers.all()) == [person] so every created
row is checked rather than just the first two.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 2402320d-04c8-4ddf-8a6e-d2bde93feda0

📥 Commits

Reviewing files that changed from the base of the PR and between 2293b5b and d7cfd19.

📒 Files selected for processing (1)
  • tests/test_baker.py

@benaduo benaduo force-pushed the fix/bulk-create-m2m-related-name-test branch from d7cfd19 to 5b67759 Compare March 19, 2026 16:39
…ated_name

Prior to the fix in the bulk_create pipeline, calling baker.make() with
_bulk_create=True on a model whose ManyToManyField defines an explicit
related_name would raise a TypeError. The through-table constructor was
being invoked with the related_name as a keyword argument instead of the
correct column names sourced from m2m_field_name() and
m2m_reverse_field_name().

Although the underlying bug has since been resolved, no regression test
existed to guard against a recurrence. This commit adds that test using
the Store.customers field (related_name="favorite_stores"), which is the
canonical example from the original bug report.

Changes:
- tests/test_baker.py: add test_create_with_field_with_related_name to
  TestCreateM2MWhenBulkCreate, covering baker.make(Store, customers=[...],
  _quantity=10, _bulk_create=True) and asserting M2M rows are created
  correctly for all bulk-created instances

Fixes model-bakers#489

Author: Benjamin Aduo (@benaduo)
@benaduo benaduo force-pushed the fix/bulk-create-m2m-related-name-test branch from 5b67759 to ccd776c Compare March 19, 2026 20:30
Copy link
Member

@amureki amureki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic, thank you very much for noting that it was missing a regression test and adding it! 🙌

@amureki amureki merged commit 5be6452 into model-bakers:main Mar 20, 2026
45 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants