佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > python用sqlacodegen根據(jù)已有數(shù)據(jù)庫(表)結(jié)構(gòu)生成對應(yīng)SQLAlchemy模型

python用sqlacodegen根據(jù)已有數(shù)據(jù)庫(表)結(jié)構(gòu)生成對應(yīng)SQLAlchemy模型

熱門標(biāo)簽:服務(wù)器配置 美圖手機(jī) 鐵路電話系統(tǒng) 呼叫中心市場需求 檢查注冊表項(xiàng) 智能手機(jī) 網(wǎng)站文章發(fā)布 銀行業(yè)務(wù)

今天介紹一個后臺開發(fā)神器,很適合當(dāng)我們數(shù)據(jù)庫中已存在了這些表,然后你想得到它們的model類使用ORM技術(shù)進(jìn)行CRUD操作(或者我根本就不知道怎么寫modle類的時候);
手寫100張表的model類?
這是。。。。。。。。。 是不可能的,這輩子都不可能的。
因?yàn)槲覀冇衧qlacodegen神器, 一行命令獲取數(shù)據(jù)庫所有表的模型類。

應(yīng)用場景

1、后臺開發(fā)中,需要經(jīng)常對數(shù)據(jù)庫進(jìn)行CRUD操作;

2、這個過程中,我們就經(jīng)常借助ORM技術(shù)進(jìn)行便利的CURD,比如成熟的SQLAlchemy;

3、但是,進(jìn)行ORM操作前需要提供和table對應(yīng)的模型類;

4、并且,很多歷史table已經(jīng)存在于數(shù)據(jù)庫中;

5、如果有幾百張table呢?還自己一個個去寫嗎?

6、我相信你心中會有個念頭。。。

福音

還是那句話,Python大法好。 這里就介紹一個根據(jù)已有數(shù)據(jù)庫(表)結(jié)構(gòu)生成對應(yīng)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

快快使用

使用方法也很簡單,只需要在終端(命令行窗口)運(yùn)行一行命令即可, 將會獲取到整個數(shù)據(jù)庫的model:
常用數(shù)據(jù)庫的使用方法:

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

查看具體參數(shù)可以輸入:

sqlacodegen --help

參數(shù)含義:

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的默認(rèn)的postgres數(shù)據(jù)庫中有個這樣的表:

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進(jìn)行操作,我需要獲取它的modle類但唯一索引的model類怎么寫呢? 我們借助sqlacodegen來自動生成就好了

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

模型類效果

查看輸出到models.py的內(nèi)容

# 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)

如果你有很多表,就直接指定數(shù)據(jù)庫唄(這是會生成整個數(shù)據(jù)庫的ORM模型類哦),不具體到每張表就好了, 后面就可以愉快的CRUD了,耶

注意事項(xiàng)

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

當(dāng)你的表的字段缺少primary key或這張表是有兩個外鍵約束的時候,會生成table而不是模型類了。比如,我那張表是這樣的結(jié)構(gòu):

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);

再執(zhí)行同一個命令:

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)
)

其實(shí)和模型類差不多嘛,但是還是盡量帶上primary key吧,免得手動修改成模型類

以上就是python用sqlacodegen根據(jù)已有數(shù)據(jù)庫(表)結(jié)構(gòu)生成對應(yīng)SQLAlchemy模型的詳細(xì)內(nèi)容,更多關(guān)于python sqlacodegen的使用的資料請關(guān)注腳本之家其它相關(guān)文章!

您可能感興趣的文章:
  • Python 數(shù)據(jù)結(jié)構(gòu)之樹的概念詳解
  • python三種數(shù)據(jù)結(jié)構(gòu)及13種創(chuàng)建方法總結(jié)
  • python數(shù)據(jù)結(jié)構(gòu)的排序算法
  • Python內(nèi)置數(shù)據(jù)結(jié)構(gòu)列表與元組示例詳解
  • Python二進(jìn)制數(shù)據(jù)結(jié)構(gòu)Struct的具體使用
  • Python數(shù)據(jù)結(jié)構(gòu)之圖的存儲結(jié)構(gòu)詳解
  • Python數(shù)據(jù)結(jié)構(gòu)之二叉排序樹的定義、查找、插入、構(gòu)造、刪除
  • Python數(shù)據(jù)結(jié)構(gòu)之優(yōu)先級隊(duì)列queue用法詳解
  • 詳解python數(shù)據(jù)結(jié)構(gòu)之棧stack
  • Python數(shù)據(jù)結(jié)構(gòu)詳細(xì)

標(biāo)簽:紅河 河南 長治 樂山 新疆 滄州 沈陽 上海

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《python用sqlacodegen根據(jù)已有數(shù)據(jù)庫(表)結(jié)構(gòu)生成對應(yīng)SQLAlchemy模型》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    扎赉特旗| 米林县| 广南县| 江陵县| 安西县| 库车县| 东阿县| 安平县| 沙田区| 兴隆县| 尼木县| 平塘县| 林芝县| 龙川县| 漳州市| 阿克陶县| 吴旗县| 昔阳县| 双峰县| 墨江| 棋牌| 四平市| 岑巩县| 阜新市| 永新县| 盐源县| 邵阳县| 和田市| 康保县| 石景山区| 三河市| 榕江县| 平潭县| 合山市| 修武县| 汤阴县| 林芝县| 新密市| 富裕县| 四平市| 邛崃市|