GithubHelp home page GithubHelp logo

Comments (2)

RobertRosca avatar RobertRosca commented on May 13, 2024

Would PR #43 solve this when merged?

from sqlmodel.

aghanti7 avatar aghanti7 commented on May 13, 2024

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?

from sqlmodel.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.