sqlalchemy automap 和 reflect

用法

指定表查询

反射部分表

1
2
3
4
5
6
7
8
test1_engine = create_engine(test1_db_uri, echo=True)
test2_engine = create_engine(test2_dburi, echo=True)
test1_metadata = MetaData()
test2_metadata = MetaData()
test1_metadata(bind=test1, only=['table1', 'table2'])
test2_metadata(bind=oms_metadata, only=['table1', 'table2', 'table3'])
test1_session = Session(test1_engine)
test2_session = Session(test2_engine)

也可以这样

1
MyTable = Table('target_table', metadata, autoload=True, autoload_with=engine)

查询

1
2
3
4
session = Session(test1_engine)
Table1 = odc_metadata.tables.get("table1")
test1_session(Table1).filter_by(id=id).first()
test1_session(Table1).filter(Table1.id==id).first()

来世做李白

social