欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2021年06月15日 10:39:46   作者:一擼程猿  
本文介紹了如何使用sqlacodegen獲取數(shù)據(jù)庫(kù)所有表的模型類,然后使用ORM技術(shù)進(jìn)行CRUD操作,有此需求的朋友可以了解下本文

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

應(yīng)用場(chǎng)景

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

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

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

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

5、如果有幾百?gòu)坱able呢?還自己一個(gè)個(gè)去寫嗎?

6、我相信你心中會(huì)有個(gè)念頭。。。

福音

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

快快使用

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

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ù)庫(kù)中有個(gè)這樣的表:

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來(lái)自動(dòng)生成就好了

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ù)庫(kù)唄(這是會(huì)生成整個(gè)數(shù)據(jù)庫(kù)的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或這張表是有兩個(gè)外鍵約束的時(shí)候,會(huì)生成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í)行同一個(gè)命令:

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吧,免得手動(dòng)修改成模型類

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

相關(guān)文章

  • python commands模塊的適用方式

    python commands模塊的適用方式

    這篇文章主要介紹了python commands模塊的適用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • 7個(gè)有用的Pandas顯示選項(xiàng)分享

    7個(gè)有用的Pandas顯示選項(xiàng)分享

    Pandas是一個(gè)在數(shù)據(jù)科學(xué)中常用的功能強(qiáng)大的Python庫(kù)。在某些情況下,我們可能希望更改所顯示內(nèi)容的格式,這就需要使用Pandas的一些定制功能來(lái)幫助我們自定義內(nèi)容的顯示方式。本文為大家總結(jié)了7個(gè)有用的Pandas顯示選項(xiàng),希望對(duì)大家有所幫助
    2022-12-12
  • Python利器openpyxl之操作excel表格

    Python利器openpyxl之操作excel表格

    這篇文章主要給大家介紹了關(guān)于Python利器openpyxl之操作excel表格的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • TensorFlow的reshape操作 tf.reshape的實(shí)現(xiàn)

    TensorFlow的reshape操作 tf.reshape的實(shí)現(xiàn)

    這篇文章主要介紹了TensorFlow的reshape操作 tf.reshape的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • Python PIL實(shí)現(xiàn)GIF壓縮工具

    Python PIL實(shí)現(xiàn)GIF壓縮工具

    本文將結(jié)合wxPython的GUI框架和PIL(Python Imaging Library)的圖像處理能力編寫一個(gè)GIF壓縮工具,并提供了兩種壓縮方式,感興趣的小伙伴可以了解下
    2024-10-10
  • Python生成pdf目錄書(shū)簽的實(shí)例方法

    Python生成pdf目錄書(shū)簽的實(shí)例方法

    在本篇文章里小編給大家整理了關(guān)于Python生成pdf目錄書(shū)簽的實(shí)例方法,有需要的朋友們可以學(xué)習(xí)下。
    2020-10-10
  • 使用 Python 創(chuàng)建一個(gè)基于規(guī)則的聊天機(jī)器人

    使用 Python 創(chuàng)建一個(gè)基于規(guī)則的聊天機(jī)器人

    這篇文章主要介紹了使用 Python 創(chuàng)建一個(gè)基于規(guī)則的聊天機(jī)器人,使用 Python 創(chuàng)建一個(gè)簡(jiǎn)單的基于規(guī)則的聊天機(jī)器人 聊天機(jī)器人本身是一種機(jī)器或軟件,它通過(guò)文本或句子模仿人類交互。 簡(jiǎn)而言之,可以使用類似于與人類對(duì)話的軟件進(jìn)行聊天。
    2021-10-10
  • Python實(shí)現(xiàn)多視頻畫面拼接

    Python實(shí)現(xiàn)多視頻畫面拼接

    這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)多視頻畫面拼接功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-10-10
  • Python遞歸函數(shù)反轉(zhuǎn)序列的實(shí)現(xiàn)

    Python遞歸函數(shù)反轉(zhuǎn)序列的實(shí)現(xiàn)

    本文主要介紹了Python遞歸函數(shù)反轉(zhuǎn)序列的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • 用TensorFlow實(shí)現(xiàn)lasso回歸和嶺回歸算法的示例

    用TensorFlow實(shí)現(xiàn)lasso回歸和嶺回歸算法的示例

    本篇文章主要介紹了用TensorFlow實(shí)現(xiàn)lasso回歸和嶺回歸算法的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-05-05

最新評(píng)論