tortoise / tortoise/tortoise-orm

Model in Built Filters (polymorphic identity?)

Open
#343 4 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

Problem I'm trying to solve
I have a table that really contains multiple item types. In SQLAlchemy I would make multiple models from this using Polymorphic Identity. I can't figure out how to do so w/Tortoise or if there exists an API for this at all.

Desired Solution
I would like some method to build multiple models from one table based on inbuilt where statements. I also want model associated inbuilt filters in general. For example, deleted_flag='N' so that when using a model the filter is already present. The reason being I'm trying to make these models abstract complexity for more junior devs and automation support.

Alternatives Considered
I have made this happen via SQLAlchemy. Here's an example:

class ExecutionPlanModel(Base):
    __tablename__ = 'tesexecplan'
    __table_args__ = {"schema": "tesreport"}

    objref = Column(String, primary_key=True)
    expt_id = Column(String, primary_key=True)

    __mapper_args__ = {
        "polymorphic_on": case([
            (expt_id.like('MP%'), "maskplan"),
            (expt_id.like('DMLP%'), "dmlp"),
            (expt_id.like('DP%'), "device"),
        ], else_='dmlp'),
        "polymorphic_identity": "executionplanmodel"
    }

And a concrete class from the polymorphic base.

class MaskPlanModel(ExecutionPlanModel):

    maskname = Column(String, name='masklayername')
    tapeoutprojectobjref = Column(String, ForeignKey('tesreport.testapeoutproject.objref'))

    tapeoutproject = relationship("TapeoutProjectModel", back_populates="masks")
    # dmlps = relationship('MPAssociation')

    __mapper_args__ = {
        'polymorphic_identity': 'maskplan',
    }

    def __repr__(self):
        return f"<MaskPlan(expt_id='{self.expt_id}')>"

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

No Tortoise files, tests, or entry points are named. Start by reviewing the SQLAlchemy polymorphic identity examples in the issue and the existing Tortoise model-inheritance and filtering APIs; clarify whether both filtered models and polymorphic identities are in scope. Done would require an agreed design and tests covering the requested behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, sqlalchemy
Domain
backend, databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.