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
fromclickhouse_sqlalchemy.ext.declarativeimportdeclarative_basefromsqlalchemyimportColumnfromsqlalchemyimportInteger, Stringfromclickhouse_sqlalchemyimportenginesfromsqlalchemyimportcreate_enginefromsqlalchemy.ormimportsessionmakerfromsqlalchemyimportselectfromsqlalchemy_utilsimportcreate_viewBase=declarative_base()
classTableTest(Base):
__tablename__='tabletest'id=Column(Integer, primary_key=True)
testcontent=Column(String)
remark=Column(String)
__table_args__= (
engines.MergeTree(order_by='id', primary_key='id'),
{'comment': 'table used for testing'}
)
viewlist= []
viewlist.extend([
TableTest.id.label('id'),
TableTest.testcontent.label('testcontent'),
TableTest.remark.label('remark'),
])
stmt_view=select(viewlist)
cvView=create_view('viewTest', stmt_view, Base.metadata)
classViewTest(Base):
__tablename__='viewTest'__table__=cvView__table_args__= {'comment': 'view used for testing'}
# clickhouse machine infockuser="ckuser"ckpwd="ckpassword"ckip="ckhost"ckport="8123"ckdbname='ckdb'uri=f"clickhouse://{ckuser}:{ckpwd}@" \
f"{ckip}:{ckport}/{ckdbname}"engine=create_engine(uri, echo=False)
DBsession=sessionmaker(bind=engine)
session=DBsession()
print(session)
session.execute('SELECT 1')
# TableTest.__table__.create(engine) # success create table tableTest# ViewTest.__table__.create(engine) # sqlalchemy.exc.CompileError: No engine for table 'viewTest'Base.metadata.create_all(bind=engine) # success create both table tableTest and view viewTestsession.close()
So after creating TableTest with code
TableTest.__table__.create(engine)
How to individually create view ViewTest?
The text was updated successfully, but these errors were encountered:
flyly0755
changed the title
With method create_view to construct a view ORM, then how to create corresponding database view?
With method create_view to construct a view ORM, then how to create corresponding database view?
Jun 11, 2024
I'd like to bump this, and would like to add that it also maybe a bug:
ViewTest.__table__.create(engine)
This code should be useable as a View is just a special type of Table for sqlalchemy, which is the reason why it gets decalared in a similar way to it using create_view(). But if I execute this, it results in the creation of a table and not a view, which cannot be intended.
In addition the following should work, but results in an error because the view is created not respecting the arguments given using the tables argument.
So after creating TableTest with code
How to individually create view ViewTest?
The text was updated successfully, but these errors were encountered: