地址
https://pypi.org/project/sqlacodegen/
https://github.com/agronholm/sqlacodegen
安装
pip install sqlacodegen
使用
sqlacodegen postgresql:///some_local_db
sqlacodegen mysql+oursql://user:password@localhost/dbname
sqlacodegen sqlite:///database.db
使用案例:
//将生成的代码显示到控制台
sqlacodegen mysql+pymysql://数据库连接名:数据库连接密码@127.0.0.1/数据库名字?charset=utf8mb4
//将生成的代码存储到文件
sqlacodegen mysql+pymysql://数据库连接名:数据库连接密码@127.0.0.1/数据库名字?charset=utf8mb4 --outfile "ModelsMyql.py"
生成代码示例:
class Car(Base):
__tablename__ = 'car'
id = Column(Integer, primary_key=True)
license_plate = Column(VARCHAR(255), comment='车牌号')
driver_name = Column(VARCHAR(50), comment='驾驶人')
industrial_internet_id = Column(VARCHAR(255), comment='互联网唯一标识')
class Student(Base):
__tablename__ = 'students'
id = Column(Integer, primary_key=True)
first_name = Column(String(255))
last_name = Column(String(255))
age = Column(Integer)
其他选项
optional arguments:
-h, --help show this help message and exit
--version print the version number and exit
--schema SCHEMA load tables from an alternate schema
--tables TABLES tables to process (comma-separated, default: all)
--noviews ignore views
--noindexes ignore indexes
--noconstraints ignore constraints
--nojoined don't autodetect joined table inheritance
--noinflect don't try to convert tables names to singular form
--noclasses don't generate classes, only tables
--nocomments don't render column comments
--outfile OUTFILE file to write output to (default: stdout)