tortoise / tortoise/tortoise-orm
Support symmetrical ManyToMany relationships
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 5.6k
- Forks
- 516
- Avg merge
- 2d 21h
- Merged PRs (30d)
- 9
Description
Django ORM has support for what they call symmetrical relationships, that is a ManyToMany relationship on self. (see here https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ManyToManyField.symmetrical)
For example, if you want a ManyToMany relation for friends and you want the logic to be "if I am your friend, then you are my friend", you would use a symmetrical relation. That way, there is no related attribute generated on the other class, and for checking the friend status, you only have to look up one field instead of both.
Example
class User(models.Model):
name = fields.TextField()
friends = fields.ManyToMany("models.User", symmetrical=True)
>>> u1 = await User.create("Bob")
>>> u2 = await User.create("Alice")
>>> await u1.friends.add(u2)
>>> await u1.friends.filter(id=u2.id).exists()
True
>>> # bi-directional
>>> await u2.friends.filter(id=u1.id).exists()
True
I can try to contribute this, but I want the confirmation from a maintainer first
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating Tortoise ORM's ManyToMany implementation and its relationship tests, then compare the requested self-referential behavior with Django's symmetrical option. Done means a self-referential ManyToMany relationship can be declared with symmetrical behavior, adding a relation through one instance makes it visible from the other, and the related attribute behavior matches the issue's example.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- database
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100