Skip to content

Conversation

@scriptogre
Copy link
Contributor

Description

Add create() method to ReverseRelation for ForeignKeyField.

Provide an ergonomic way to create related objects, and better fulfill expectations of people coming from Django's ORM.

Before:

event = await Event.create(tournament=tournament, ...)

After:

event = await tournament.events.create(...)  # Now possible

The implementation is simple (just ~5 lines of code):

  1. Check if parent instance is saved in db (otherwise raise OperationalError)
  2. Automatically inject relationship into kwargs
  3. Call remote model's create method (with the updated kwargs)

Motivation and Context

  1. Django compatibility: Developers migrating from Django probably expect parent.children.create() to work.
  2. Consistency: Tortoise already implements such methods for ManyToMany fields (.add(), .clear(), .remove()). ForeignKey should have the same level of support.
  3. Developer Experience: Eliminates the need to manually specify the foreign key relationship, reducing boilerplate.

How Has This Been Tested?

Added tests for successful creation and error handling with unsaved instances.

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added the changelog accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@codspeed-hq
Copy link

codspeed-hq bot commented Aug 18, 2025

CodSpeed Performance Report

Merging #1991 will not alter performance

Comparing scriptogre:enable-create-via-reverse-relations (4c495a5) with develop (c4f601e)

Summary

✅ 16 untouched benchmarks

@henadzit
Copy link
Contributor

I just merged a fix for CI. Can you please rebase your branch?

…ate()`

The create method automatically sets the foreign key field and validates that the parent instance is saved.

Example: `tournament.events.create(...)`
@scriptogre scriptogre force-pushed the enable-create-via-reverse-relations branch from bda186c to 4c495a5 Compare August 20, 2025 08:01
@coveralls
Copy link

Pull Request Test Coverage Report for Build 17092327155

Details

  • 5 of 5 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall first build on enable-create-via-reverse-relations at 89.637%

Totals Coverage Status
Change from base Build 17090868824: 89.6%
Covered Lines: 6676
Relevant Lines: 7266

💛 - Coveralls

Copy link
Contributor

@henadzit henadzit left a comment

Choose a reason for hiding this comment

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

LGTM

@henadzit henadzit merged commit cbd1fb5 into tortoise:develop Aug 20, 2025
9 checks passed
@scriptogre scriptogre deleted the enable-create-via-reverse-relations branch August 22, 2025 09:55
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.

3 participants