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 clickhouse-sqlalchemy support integrating with flask-sqlalchemy? #251

Open
flyly0755 opened this issue Jun 13, 2023 · 2 comments
Open

Comments

@flyly0755
Copy link

Describe the bug
With flask-sqlalchemy, ORM class showed as below, inherit db.Model where db come from SQLAlchemy(app)

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///example.sqlite"
db = SQLAlchemy(app)

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String, unique=True, nullable=False)

but clickhouse-sqlalchemy syntax showed as below,ORM class inherit Base which come from get_declarative_base method.

Base = get_declarative_base(metadata=metadata)

class Rate(Base):
    day = Column(types.Date, primary_key=True)
    value = Column(types.Int32)

    __table_args__ = (
        engines.Memory(),
    )

Raise a issue under flask-sqlalchemy
pallets-eco/flask-sqlalchemy#1220
Seem clickhouse-sqlalchemy need to coordinate with flask-sqlalchemy if want to use clickhouse this database under Flask framework.
To Reproduce

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
# app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///example.sqlite"
app.config['SQLALCHEMY_DATABASE_URI'] = \
    "clickhouse://{}:{}@{}:{}/{}".format('xxx', 'yyy', 'zzz', 8123, 'aaa')  // change sqlite as above to clickhouse uri
db = SQLAlchemy(app)

class User(db.Model): # only inherit db.Model not work
# class User(db.Model, base):  inherit both Base and db.Model also not work
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String, unique=True, nullable=False)
    # __table_args__ = (engines.MergeTree(order_by='id', primary_key='id'))  # when inherit with Base, add this still not work
with app.app_context():
    db.create_all()

    db.session.add(User(username="example"))
    db.session.commit()

    users = db.session.execute(db.select(User)).scalars()

Expected behavior
can use clickhouse-sqlalchemy under Flask framework and database clickhouse

Versions

Python version: 3.9.13
Flask-SQLAlchemy version: 2.2.3
SQLAlchemy version:1.4.48

  • clickhouse-sqlalchemy==0.2.3
@xzkostyan
Copy link
Owner

@flyly0755 what kind of coordination do you expect? clickhouse-sqlalchemy is not compatible with flask-sqlalchemy yet.

clickhouse-sqlalchemy need custom metaclass ClickHouseDeclarativeMeta for declarative_base function call. You need to find the way to pass it or wrap over flask-sqlalchemy SQLAlchemy object.

@flyly0755
Copy link
Author

i hope in the future clickhouse-sqlalchemy can be compatible with flask-sqlalchemy:), so clickhouse-sqlalchemy package and clickhouse database can be used in more situation related with web product development. It will be convenient to use clickhouse-sqlalchemy in flask framework(since Django framework use itsself ORM system, usually doesn't clickhouse-sqlalchemy in Django)

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

No branches or pull requests

2 participants