首页 > 代码库 > sqlacodegen

sqlacodegen

这个工具可以把数据库的表转成sqlalchemy用的class。但是只有表有primary key才可以转化为class 否则就是Table

 

root@rijx:/tmp# sqlacodegen --schema rdb  mysql://root:passw0rd@localhost:3306# coding: utf-8from sqlalchemy import Column, Integer, String, Tablefrom sqlalchemy.ext.declarative import declarative_baseBase = declarative_base()metadata = Base.metadatat_mgr = Table(    mgr, metadata,    Column(id, Integer, nullable=False),    Column(name, String(18)),    schema=rdb)class T1(Base):    __tablename__ = t1    __table_args__ = {uschema: 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__ = {uschema: rdb}    user_id = Column(Integer, primary_key=True)    user_name = Column(String(40))    password = Column(String(10))

 

sqlacodegen