简体   繁体   中英

CreatedAt mixin (base class) in python SQLModel

I'm using SQLModel and would like to define base class with created_at field to inherit from it in others models.

class CreatedAtMixin(SQLModel):
    created_at: datetime = Field(default=datetime.now(timezone.utc), nullable=False)

But when i'm trying to use it in other model like this and making migrations via alembic :

class NetworkBase(SQLModel, CreatedAtMixin):
    name: str = Field(max_length=64, description='Name of network')
    short_name: str = Field(max_length=32, description='Short name of the network')

Im facing a problem:

Traceback (most recent call last):
  File "/Users/nikitasamaev/PycharmProjects/new-checkout-backend/venv/bin/alembic", line 8, in <module>
    sys.exit(main())
  File "/Users/nikitasamaev/PycharmProjects/new-checkout-backend/venv/lib/python3.9/site-packages/alembic/config.py", line 588, in main
    CommandLine(prog=prog).main(argv=argv)
  File "/Users/nikitasamaev/PycharmProjects/new-checkout-backend/venv/lib/python3.9/site-packages/alembic/config.py", line 582, in main
    self.run_cmd(cfg, options)
  File "/Users/nikitasamaev/PycharmProjects/new-checkout-backend/venv/lib/python3.9/site-packages/alembic/config.py", line 559, in run_cmd
    fn(
  File "/Users/nikitasamaev/PycharmProjects/new-checkout-backend/venv/lib/python3.9/site-packages/alembic/command.py", line 227, in revision
    script_directory.run_env()
  File "/Users/nikitasamaev/PycharmProjects/new-checkout-backend/venv/lib/python3.9/site-packages/alembic/script/base.py", line 563, in run_env
    util.load_python_file(self.dir, "env.py")
  File "/Users/nikitasamaev/PycharmProjects/new-checkout-backend/venv/lib/python3.9/site-packages/alembic/util/pyfiles.py", line 92, in load_python_file
    module = load_module_py(module_id, path)
  File "/Users/nikitasamaev/PycharmProjects/new-checkout-backend/venv/lib/python3.9/site-packages/alembic/util/pyfiles.py", line 108, in load_module_py
    spec.loader.exec_module(module)  # type: ignore
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "app/db/migrations/env.py", line 12, in <module>
    from app.models import *
  File "/Users/nikitasamaev/PycharmProjects/new-checkout-backend/app/models/__init__.py", line 1, in <module>
    from app.models.blockchain import Network
  File "/Users/nikitasamaev/PycharmProjects/new-checkout-backend/app/models/blockchain.py", line 9, in <module>
    class NetworkBase(SQLModel, CreatedAtMixin):
  File "/Users/nikitasamaev/PycharmProjects/new-checkout-backend/venv/lib/python3.9/site-packages/sqlmodel/main.py", line 277, in __new__
    new_cls = super().__new__(cls, name, bases, dict_used, **config_kwargs)
  File "pydantic/main.py", line 149, in pydantic.main.ModelMetaclass.__new__
  File "pydantic/config.py", line 118, in pydantic.config.inherit_config
TypeError: Cannot create a consistent method resolution
order (MRO) for bases Config, Config

Of course I can set field created_at in my NetworkBase and others models manually, it would work without any problems, but i'd like to inherit from CreatedAtMixin .

What are the ways to solve the problem?

You need to take out the SQLModel inheritance, as it is already an SQLModel class in the first class:

class CreatedAtMixin(SQLModel):
    created_at: datetime = Field(default=datetime.now(timezone.utc), nullable=False)

class NetworkBase(CreatedAtMixin):
    name: str = Field(max_length=64, description='Name of network')
    short_name: str = Field(max_length=32, description='Short name of the network')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM