tortoise / tortoise/tortoise-orm

Check if the model has been created multiple times

Open
#684 0 comments 0 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

Is your feature request related to a problem? Please describe.
When I import a model file created by someone else, will there be an alarm or error handling if the same name of the model appears?

Describe the solution you'd like
Before introducing a new model, check to see if the model already exists.
In code:
tortoise.init.py 417

cls.apps[app_label] = {model.__name__: model for model in app_models}
When there are multiple apps in the same database, it is necessary to check whether there is the same model name.


@classmethod
    def _init_apps(cls, apps_config: dict) -> None:
        for name, info in apps_config.items():
            try:
                cls.get_connection(info.get("default_connection", "default"))
            except KeyError:
                raise ConfigurationError(
                    'Unknown connection "{}" for app "{}"'.format(
                        info.get("default_connection", "default"), name
                    )
                )

            cls.init_models(info["models"], name, _init_relations=False) # need check model.

            for model in cls.apps[name].values():
                model._meta.default_connection = info.get("default_connection", "default")

        cls._init_relations()

        cls._build_initial_querysets()

Describe alternatives you've considered
I don't know if it's a problem to pay attention to. If so, I can fix the problem

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 in tortoise/init.py at _init_apps and the model registration around line 417. Trace how models from multiple apps are stored, then determine the expected behavior when names collide. Done means duplicate model names are detected during initialization and handled consistently, with tests added for multiple apps sharing a model name.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
databases
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.