在SQLAlchemy ORM中动态变更表名
时间:2021-07-01 10:21:17
帮助过:67人阅读
sqlalchemy
import create_engine
from sqlalchemy
import Table, Column, Integer, String, MetaData
from sqlalchemy.orm
import sessionmaker
metadata =
MetaData()
from sqlalchemy.ext.declarative
import declarative_base
Base =
declarative_base()
class User(Base):
__tablename__ =
‘users‘
__table_args__ =
{
‘mysql_engine‘:
‘InnoDB‘,
‘mysql_charset‘:
‘utf8‘
}
id = Column(Integer, primary_key=
True)
name = Column(String(30
))
fullname = Column(String(120
))
password = Column(String(30
))
engine = create_engine(
‘mysql://root:@127.0.0.1/test?charset=utf8‘, echo=
True)
Session = sessionmaker(bind=
engine)
session = Session()
查询表1
user = session.query(User).filter(User.name==‘myname‘).all()
查询表2
User.__table__.name = ‘users_1‘
user = session.query(User).filter(User.name==‘myname‘).all()
在SQLAlchemy ORM中动态变更表名
标签:column 127.0.0.1 root ESS 需要 mys 查询 query 其他