tortoise / tortoise/tortoise-orm

refresh_from_db should refresh related models that have already been fetched

Open
#1,258 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.6k
Forks
516
Avg merge
2d 21h
Merged PRs (30d)
9

Description

It is a little misleading that you can fetch an instance of a model from the db, and also fetch it's related models, but then when you call refresh_from_db() on the instance, it only refreshes the fields from the model's table, and does not fetch the related models.

What's worse (and almost makes me think this should be reported as a bug rather than a feature request) is that if you call clear() on a many_to_many relationship for the instance, though it does remove the rows from the relationship table, it does not update your current instance. Meaning, that if you:

  1. fetch a model instance from the db,
  2. also fetch related models for that instance
  3. then call clear() to remove all the relationships
  4. then add new related models to the instance
  5. then call refresh_from_db() on the instance

the end result is that if you then check the instance for its related models, instead of getting back the newly linked relations, or getting back an error that they're not fetched, you just get back the old ones which have already been cleared. This is unintuitive.

It would be really nice if clear() actually cleared the relationship from the current model instance. It would also be nice if refresh_from_db automatically refreshed relation models, or at the very least if it allowed you to optionally specify which relationships should also be refreshed. Then it would be obvious from the docs what's going to happen.

A workaround is to do something like this:

await my_instance.get(id=my_instance.id).prefetch_related("my_related_model")

But would be much nicer if you could do something like:

await my_instance.refresh_from_db(["my_related_model"])

or even if you could just do:

await my_instance.refresh_from_db().prefetch_related("my_related_model")

Which I tried, but of course does not work

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the implementations of refresh_from_db(), relation fetching, and many-to-many clear() behavior in the ORM. Compare the documented and current behavior for prefetched relations, then define a consistent refresh API and verify that cleared and newly linked relations are reflected on the instance.

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
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.