SQLAlchemy model代码生成工具

sqlacodegen是一个用于从现有数据库生成SQLAlchemyORM模型代码的工具。用户可以通过pip安装,然后提供数据库连接信息来生成Python代码。它可以支持多种数据库,如PostgreSQL、MySQL和SQLite。生成的代码示例包含了Car和Student类,每个类对应数据库中的一个表。此外,工具还提供多个可选参数,如指定输出文件、忽略视图或索引等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SQLAlchemy model代码生成工具

地址

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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

飞得更高肥尾沙鼠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值