Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Does flask-sqlalchemy support database clickhouse or not? #1220

Closed
flyly0755 opened this issue Jun 12, 2023 · 3 comments
Closed

Does flask-sqlalchemy support database clickhouse or not? #1220

flyly0755 opened this issue Jun 12, 2023 · 3 comments

Comments

@flyly0755
Copy link

flyly0755 commented Jun 12, 2023

I want to use clickhouse as a backend database in flask project.
Want to make sure whether flask-sqlalchemy support clickhouse?

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):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String, unique=True, nullable=False)

with app.app_context():
    db.create_all()

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

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

Raised error:
db = SQLAlchemy(app) -- trigger by this code
...
xxx/site-packages/clickhouse_sqlalchemy/sql/schema.py", line 2, in
from sqlalchemy.sql.base import (
ImportError: cannot import name '_bind_or_error' from 'sqlalchemy.sql.base' (xxx/site-packages/sqlalchemy/sql/base.py)

Environment:

  • Python version: 3.9.13
  • Flask-SQLAlchemy version: 2.2.3
  • SQLAlchemy version:2.0.16
    - clickhouse-sqlalchemy==0.2.3
@RonnyPfannschmidt
Copy link

As per https://clickhouse-sqlalchemy.readthedocs.io/en/latest/

Use SQLalchemy 1.4.x

@flyly0755
Copy link
Author

SQLalchemy

Bingo, you are right:)
but after i change SQLalchemy as 1.4.48, with other issues,
seems not easy to reorganize the code
https://github.com/xzkostyan/clickhouse-sqlalchemy
with clickhouse-sqlalchemy, the table create syntax showed as below:

from sqlalchemy import create_engine, Column, MetaData

from clickhouse_sqlalchemy import (
    Table, make_session, get_declarative_base, types, engines
)

uri = 'clickhouse+native://localhost/default'

engine = create_engine(uri)
session = make_session(engine)
metadata = MetaData(bind=engine)

Base = get_declarative_base(metadata=metadata)

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

    __table_args__ = (
        engines.Memory(),
    )

how to integrate with flask_sqlalchemy db.Model?

@ThiefMaster
Copy link
Contributor

It looks like they didn't write a proper sqlalchemy engine but do their own stuff on top of it. Looks like a mess tbh.

@pallets-eco pallets-eco locked and limited conversation to collaborators Jun 12, 2023
@davidism davidism converted this issue into discussion #1221 Jun 12, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Development

No branches or pull requests

4 participants