主页 > 知识库 > python用sqlacodegen根据已有数据库(表)结构生成对应SQLAlchemy模型

python用sqlacodegen根据已有数据库(表)结构生成对应SQLAlchemy模型

热门标签:海外网吧地图标注注册 孝感营销电话机器人效果怎么样 地图标注自己和别人标注区别 腾讯地图标注没法显示 商家地图标注海报 打电话机器人营销 ai电销机器人的优势 南阳打电话机器人 聊城语音外呼系统

今天介绍一个后台开发神器,很适合当我们数据库中已存在了这些表,然后你想得到它们的model类使用ORM技术进行CRUD操作(或者我根本就不知道怎么写modle类的时候);
手写100张表的model类?
这是。。。。。。。。。 是不可能的,这辈子都不可能的。
因为我们有sqlacodegen神器, 一行命令获取数据库所有表的模型类。

应用场景

1、后台开发中,需要经常对数据库进行CRUD操作;

2、这个过程中,我们就经常借助ORM技术进行便利的CURD,比如成熟的SQLAlchemy;

3、但是,进行ORM操作前需要提供和table对应的模型类;

4、并且,很多历史table已经存在于数据库中;

5、如果有几百张table呢?还自己一个个去写吗?

6、我相信你心中会有个念头。。。

福音

还是那句话,Python大法好。 这里就介绍一个根据已有数据库(表)结构生成对应SQLAlchemy模型类的神器: sqlacodegen

This is a tool that reads the structure of an existing database and generates the appropriate SQLAlchemy model code, using the declarative style if possible.

安装方法:

pip install sqlacodegen

快快使用

使用方法也很简单,只需要在终端(命令行窗口)运行一行命令即可, 将会获取到整个数据库的model:
常用数据库的使用方法:

sqlacodegen postgresql:///some_local_db
sqlacodegen mysql+oursql://user:password@localhost/dbname
sqlacodegen sqlite:///database.db

查看具体参数可以输入:

sqlacodegen --help

参数含义:

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
  --outfile OUTFILE  file to write output to (default: stdout)

目前我在postgresql的默认的postgres数据库中有个这样的表:

create table friends
(
  id   varchar(3) primary key ,
  address  varchar(50) not null ,
  name varchar(10) not null
);

create unique index name_address
on friends (name, address);

为了使用ORM进行操作,我需要获取它的modle类但唯一索引的model类怎么写呢? 我们借助sqlacodegen来自动生成就好了

sqlacodegen postgresql://ridingroad:ridingroad@127.0.0.1:5432/postgres --outfile=models.py  --tables friends

模型类效果

查看输出到models.py的内容

# coding: utf-8
from sqlalchemy import Column, Index, String
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()
metadata = Base.metadata


class Friend(Base):
    __tablename__ = 'friends'
    __table_args__ = (
        Index('name_address', 'name', 'address', unique=True),
    )

    id = Column(String(3), primary_key=True)
    address = Column(String(50), nullable=False)
    name = Column(String(10), nullable=False)

如果你有很多表,就直接指定数据库呗(这是会生成整个数据库的ORM模型类哦),不具体到每张表就好了, 后面就可以愉快的CRUD了,耶

注意事项

Why does it sometimes generate classes and sometimes Tables?

Unless the --noclasses option is used, sqlacodegen tries to generate declarative model classes from each table. There are two circumstances in which a Table is generated instead: 1、the table has no primary key constraint (which is required by SQLAlchemy for every model class) 2、the table is an association table between two other tables

当你的表的字段缺少primary key或这张表是有两个外键约束的时候,会生成table而不是模型类了。比如,我那张表是这样的结构:

create table friends
(
  id   varchar(3) ,
  address  varchar(50) not null ,
  name varchar(10) not null
);

create unique index name_address
  on friends (name, address);

再执行同一个命令:

sqlacodegen postgresql://ridingroad:ridingroad@127.0.0.1:5432/postgres --outfile=models.py  --tables friends

获取到的是Table:

# coding: utf-8
from sqlalchemy import Column, Index, MetaData, String, Table

metadata = MetaData()


t_friends = Table(
    'friends', metadata,
    Column('id', String(3)),
    Column('address', String(50), nullable=False),
    Column('name', String(10), nullable=False),
    Index('name_address', 'name', 'address', unique=True)
)

其实和模型类差不多嘛,但是还是尽量带上primary key吧,免得手动修改成模型类

以上就是python用sqlacodegen根据已有数据库(表)结构生成对应SQLAlchemy模型的详细内容,更多关于python sqlacodegen的使用的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
  • Python 数据结构之树的概念详解
  • python三种数据结构及13种创建方法总结
  • python数据结构的排序算法
  • Python内置数据结构列表与元组示例详解
  • Python二进制数据结构Struct的具体使用
  • Python数据结构之图的存储结构详解
  • Python数据结构之二叉排序树的定义、查找、插入、构造、删除
  • Python数据结构之优先级队列queue用法详解
  • 详解python数据结构之栈stack
  • Python数据结构详细

标签:抚州 南宁 牡丹江 聊城 六盘水 扬州 杨凌 迪庆

巨人网络通讯声明:本文标题《python用sqlacodegen根据已有数据库(表)结构生成对应SQLAlchemy模型》,本文关键词  python,用,sqlacodegen,根据,;如发现本文内容存在版权问题,烦请提供相关信息告之我们,我们将及时沟通与处理。本站内容系统采集于网络,涉及言论、版权与本站无关。
  • 相关文章
  • 下面列出与本文章《python用sqlacodegen根据已有数据库(表)结构生成对应SQLAlchemy模型》相关的同类信息!
  • 本页收集关于python用sqlacodegen根据已有数据库(表)结构生成对应SQLAlchemy模型的相关信息资讯供网民参考!
  • 企业400电话

    智能AI客服机器人
    15000

    在线订购

    合计11份范本:公司章程+合伙协议+出资协议+合作协议+股权转让协议+增资扩股协议+股权激励+股东会决议+董事会决议

    推荐文章