这个工具可以把数据库的表转成sqlalchemy用的class。但是只有表有primary key才可以转化为class 否则就是Table
root@rijx:/tmp# sqlacodegen --schema rdb mysql://root:passw0rd@localhost:3306 # coding: utf-8 from sqlalchemy import Column, Integer, String, Table from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() metadata = Base.metadata t_mgr = Table( ‘mgr‘, metadata, Column(‘id‘, Integer, nullable=False), Column(‘name‘, String(18)), schema=‘rdb‘ ) class T1(Base): __tablename__ = ‘t1‘ __table_args__ = {u‘schema‘: ‘rdb‘} id = Column(Integer, primary_key=True) val = Column(String(8)) t_t2 = Table( ‘t2‘, metadata, Column(‘id‘, Integer, nullable=False), Column(‘val‘, String(8)), schema=‘rdb‘ ) class User(Base): __tablename__ = ‘user‘ __table_args__ = {u‘schema‘: ‘rdb‘} user_id = Column(Integer, primary_key=True) user_name = Column(String(40)) password = Column(String(10))