Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to mix untyped and typed models? #200

Open
8 tasks done
pnovotnak opened this issue Dec 22, 2021 · 1 comment
Open
8 tasks done

How to mix untyped and typed models? #200

pnovotnak opened this issue Dec 22, 2021 · 1 comment
Labels
question Further information is requested

Comments

@pnovotnak
Copy link

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the SQLModel documentation, with the integrated search.
  • I already searched in Google "How to X in SQLModel" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to SQLModel but to Pydantic.
  • I already checked if it is not related to SQLModel but to SQLAlchemy.

Commit to Help

  • 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

@pnovotnak pnovotnak added the question Further information is requested label Dec 22, 2021
@ljluestc
Copy link

ljluestc commented Sep 3, 2023

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants