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

Is dynamic schema supported like in SQLAlchemy? #50

Open
8 tasks done
aghanti7 opened this issue Aug 28, 2021 · 2 comments
Open
8 tasks done

Is dynamic schema supported like in SQLAlchemy? #50

aghanti7 opened this issue Aug 28, 2021 · 2 comments
Labels
question Further information is requested

Comments

@aghanti7
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

Example code from the blog:

Base = declarative_base()

class MyTableClass(Base):
	__tablename__ = 'myTableName'
	myFirstCol = Column(Integer, primary_key=True)
	mySecondCol = Column(Integer, primary_key=True)


Base.metadata.create_table(engine)

attr_dict = {'__tablename__': 'myTableName',
	     'myFirstCol': Column(Integer, primary_key=True),
	     'mySecondCol': Column(Integer)}

Description

I am looking if SQLModel supports dynamic schema like SQLAlchemy does. Example: https://sparrigan.github.io/sql/sqla/2016/01/03/dynamic-tables.html

Operating System

macOS

Operating System Details

No response

SQLModel Version

0.0.4

Python Version

3.9.0

Additional Context

No response

@aghanti7 aghanti7 added the question Further information is requested label Aug 28, 2021
@RobertRosca
Copy link
Contributor

Would PR #43 solve this when merged?

@aghanti7
Copy link
Author

I was able to achieve this with the below workaround.

class UserActivityBase(SQLModel):
    # One table per user
    # Created first time when a user logs in perhaps
    created_ts: datetime.datetime = Field(primary_key=True)
    activity_type: str
    activity_value: float


async def create_user_activity_model(uuid: str, engine: AsyncEngine):
    class UserActivity(UserActivityBase, table=True):
        __tablename__ = 'user_activity_' + uuid
        __table_args__ = {'schema': 'user_schema',
                                       'keep_existing': True}

    # Create the table if needed
    async with engine.begin() as conn:
        await conn.run_sync(SQLModel.metadata.create_all)
    return UserActivity


class UserActivityCrud(UserActivityBase):
    pass


async def get_user_activity_model(uuid: str):
    class UserActivityCrud(UserActivityBase, table=True):
        __tablename__ = 'user_activity_' + uuid
        __table_args__ = {'schema': 'user_schema',
                                       'keep_existing': True}

    return UserActivityCrud

UserActivityBase is the base SQLModel. When you want to create the table for a new user, you can call create_user_activity_model() with the user's uuid and the engine instance. Later, when you need to get the model for a particular user, just call get_user_activity_model(uuid=<uuid>).

The only thing I am facing is a warning. When you call get_user_activity_model more than once, the below warning is thrown.

/Users/ghanti/code/proj/venv/lib/python3.9/site-packages/sqlmodel/main.py:367: SAWarning: This declarative base already contains a class with the same class name and module name as app.models.UserActivityCrud, and will be replaced in the string-lookup table.

I feel this can be ignored. @tiangolo can you please confirm?

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