maxtepkeev / architect

A set of tools which enhances ORMs written in Python with more features
Other
391 stars 57 forks source link

Can I use this library for flask_sqlalchemy? #66

Closed mcauto closed 5 years ago

mcauto commented 5 years ago
# APP = create_app(mode=os.getenv("ENV", "dev"))
# DB.init_app(APP)
@architect.install('partition', type='range', subtype='date', constraint='month', column='created')
class TodoHistory(DB.Model):
    __tablename__ = "todo_history"
    __table_args__ = {'mysql_collate': 'utf8_general_ci'}

    id = DB.Column("todo_id", DB.Integer, primary_key=True)
    description = DB.Column("description", DB.String(270), nullable=False)
    created = DB.Column(DB.TIMESTAMP, server_default=text("CURRENT_TIMESTAMP"), nullable=False)

    def __init__(self, description):
        self.description = description
architect.exceptions.ORMError: Unsupported ORM "flask_sqlalchemy" requested for class "MyModel", available ORMs are: django, peewee, pony, sqlalchemy, sqlobject
from sqlalchemy.ext.declarative import declarative_base

# APP = create_app(mode=os.getenv("ENV", "dev"))
# DB.init_app(APP)

@architect.install('partition', type='range', subtype='date', constraint='month', column='created')
class TodoHistory(declarative_base(DB.Model)):
    __tablename__ = "todo_history"
    __table_args__ = {'mysql_collate': 'utf8_general_ci'}

    id = DB.Column("todo_id", DB.Integer, primary_key=True)
    description = DB.Column("description", DB.String(270), nullable=False)
    created = DB.Column(DB.TIMESTAMP, server_default=text("CURRENT_TIMESTAMP"), nullable=False)

    def __init__(self, description):
        self.description = description

image image

I solve it!

def test_history_partition(app_context):
    """ history test """
    from app.models.todo.orm import TodoHistory
    history = TodoHistory(description="test")
    import datetime
    history.created = datetime.datetime.utcnow()
    history.metadata.bind=DB.engine
    DB.session.add(history)
    DB.session.commit()
mani-coder commented 3 years ago

Hi, how did you solve this issue?

mani-coder commented 3 years ago

I was able to solve this using below config:

@architect.install(
    'partition',
    orm='sqlalchemy',
    type='range',
    subtype='date',
    constraint='month',
    column='created_at',
    db=app.config['SQLALCHEMY_DATABASE_URI']
)
awadhesh14 commented 2 years ago

@mani-coder could you elaborate?

mani-coder commented 2 years ago

@awadhesh14 for the issue mentioned in https://github.com/maxtepkeev/architect/issues/66#issue-462564069, I was able to solve it by passing additional arguments orm='sqlalchemy' and db=app.config['SQLALCHEMY_DATABASE_URI'] to the architect.install function.