How to mix untyped and typed models?
First Check
- [X] I added a very descriptive title to this issue.
- [X] I used the GitHub search to find a similar issue and didn't find it.
- [X] I searched the SQLModel documentation, with the integrated search.
- [X] I already searched in Google "How to X in SQLModel" and didn't find any information.
- [X] I already read and followed all the tutorial in the docs and didn't find an answer.
- [X] I already checked if it is not related to SQLModel but to Pydantic.
- [X] I already checked if it is not related to SQLModel but to SQLAlchemy.
Commit to Help
- [X] I commit to help with one of those options 👆
Example Code
from sqlalchemy import Column, Integer
from sqlalchemy.ext.declarative import declarative_base
from sqlmodel import SQLModel, Field
from pydantic import BaseModel
# This doesn't work. It produces:
# metaclass conflict: the metaclass of a derived class must be a (non-strict) subclass of the metaclasses of all its bases
# Base = declarative_base(cls=BaseModel)
Base = declarative_base()
class ModelBase(Base):
__abstract__ = True
def do_something(self):
pass
class MyOldModel(ModelBase):
id = Column(Integer, primary_key=True)
# This doesn't work (has no attribute '__config__')
# class MyNewModel(ModelBase, SQLModel):
# id: int = Field(primary_key=True)
Description
I'm working with a large codebase that needs to be migrated piecemeal. Is it possible to mix sqlalchemy and sqlmodel base classes?
Operating System
Linux, macOS
Operating System Details
No response
SQLModel Version
0.0.5
Python Version
Python 3.9.9
Additional Context
No response
from sqlmodel import SQLModel, Field from sqlalchemy import Column, Integer from sqlalchemy.ext.declarative import declarative_base
BaseSQLModel = declarative_base() # SQLAlchemy declarative base class
class SQLModelMixin(SQLModel): class Config: orm_mode = True
class MyOldModel(BaseSQLModel): id = Column(Integer, primary_key=True)
class MyNewModel(SQLModelMixin, BaseSQLModel): id: int = Field(primary_key=True)
Usage
old_model_instance = MyOldModel(id=1) new_model_instance = MyNewModel(id=2)
print(old_model_instance) print(new_model_instance)