tortoise / aerich

A database migrations tool for TortoiseORM, ready to production.
https://github.com/tortoise/aerich
Apache License 2.0
820 stars 94 forks source link

Allow ignoring of base models #162

Closed Kennpow closed 3 years ago

Kennpow commented 3 years ago

I haven't been able to find a way to do this, so sorry if it it exists. But currently I have a model defined as such

#### Under /app/models/base.py
...
class BaseModel(models.Model):
    created_at = fields.DatetimeField(auto_now_add=True)
    updated_at = fields.DatetimeField(auto_now_add=True)

#### Under /app/models/user.py
...
import BaseModel from /app/models/base.py

class User(BaseModel):
    id = fields.IntField(pk=True)
    email = fields.TextField()
    password = fields.TextField()
    first_name = fields.TextField()
    last_name = fields.TextField()

    class PydanticMeta:
        # Metadata for pydantic model when that gets set up
        exclude = ["password"]

#### In /app/db.py
...
TORTOISE_ORM = {
    "connections": {"default": "postgres://admin:password@localhost:5432/sample-db"},
    "apps": {
        "models": {"models": ["app.models.user", "aerich.models"]},
    },
}

def init_db(app: FastAPI):
    register_tortoise(
        app,
        config=TORTOISE_ORM,
        modules={"models": {"models": ["app.models.user"]}},
        generate_schemas=False,
        add_exception_handlers=True,
    )

Given my TORTOISE_ORM config, I was expecting that only the User model would be tracked by Aerich. But running init_db creates the BaseModel migrations as well.

I would think that the User model would be overriding the table name in the meta of BaseModel. Perhaps Aerich is picking up the BaseModel either through the import - or maybe the same folder itself?

Either way, I think it would be great if we could have a way to ignore these! And on a side question, is it possible to import app.models.*? That would be much nicer then needing to update modules each time.

long2ice commented 3 years ago

add abstract=True in base model