You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
First Check
Commit to Help
Example Code
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
The text was updated successfully, but these errors were encountered: