SQLAlchemy declarative syntax with autoload (reflection) in Pylons

后端 未结 4 1719
日久生厌
日久生厌 2020-12-16 01:57

I would like to use autoload to use an existings database. I know how to do it without declarative syntax (model/_init_.py):

def init_model(engine):         


        
4条回答
  •  一整个雨季
    2020-12-16 02:28

    from sqlalchemy import MetaData,create_engine,Table
    engine = create_engine('postgresql://postgres:********@localhost/db_name')
    
    metadata = MetaData(bind=engine)
    
    rivers = Table('rivers',metadata,autoload=True,auto_load_with=engine)
    
    from sqlalchemy import select
    
    s = select([rivers]).limit(5)
    engine.execute(s).fetchall()
    

    worked for me. I was getting the error because of not specifying bind when creating MetaData() object.

提交回复
热议问题