最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

應用場景

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

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

3、但是,進行ORM操作前需要提供和table對應的模型類;

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

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

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

福音

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

快快使用

使用方法也很簡單,只需要在終端(命令行窗口)運行一行命令即可, 將會獲取到整個數(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的默認的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進行操作,我需要獲取它的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了,耶

注意事項

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而不是模型類了。比如,我那張表是這樣的結(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)
)

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

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

相關文章

  • python連接mysql數(shù)據(jù)庫示例(做增刪改操作)

    python連接mysql數(shù)據(jù)庫示例(做增刪改操作)

    python連接mysql數(shù)據(jù)庫示例,提供創(chuàng)建表,刪除表,數(shù)據(jù)增、刪、改,批量插入操作,大家參考使用吧
    2013-12-12
  • Python打印不合法的文件名

    Python打印不合法的文件名

    這篇文章主要介紹了Python打印不合法的文件名,文中講解非常細致,代碼幫助大家更好的理解和學習,感興趣的朋友可以了解下的相關資料
    2020-07-07
  • 原理解析為什么pydantic可變對象沒有隨著修改而變化

    原理解析為什么pydantic可變對象沒有隨著修改而變化

    這篇文章主要介紹了為什么pydantic可變對象沒有隨著修改而變化的原因解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-05-05
  • Python使用pyautogui模塊實現(xiàn)自動化鼠標和鍵盤操作示例

    Python使用pyautogui模塊實現(xiàn)自動化鼠標和鍵盤操作示例

    這篇文章主要介紹了Python使用pyautogui模塊實現(xiàn)自動化鼠標和鍵盤操作,簡單描述了pyautogui模塊的功能,并結(jié)合實例形式較為詳細的分析了Python使用pyautogui模塊實現(xiàn)鼠標與鍵盤自動化操作相關技巧,需要的朋友可以參考下
    2018-09-09
  • python對日志進行處理的實例代碼

    python對日志進行處理的實例代碼

    本篇文章給大家分享了關于python處理日志的方法以及相關實例代碼,有興趣的朋友們學習下。
    2018-10-10
  • python f-string式格式化聽語音流程講解

    python f-string式格式化聽語音流程講解

    在本篇文章中小編給大家整理的是關于python f-string式格式化聽語音的相關知識點內(nèi)容,有興趣的朋友們學習下。
    2019-06-06
  • 淺談優(yōu)化Django ORM中的性能問題

    淺談優(yōu)化Django ORM中的性能問題

    這篇文章主要介紹了淺談優(yōu)化Django ORM中的性能問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • 如何利用Python實現(xiàn)簡易的音頻播放器

    如何利用Python實現(xiàn)簡易的音頻播放器

    這篇文章主要介紹了如何利用Python實現(xiàn)簡易的音頻播放器,需要用到的庫有pygame和tkinter,實現(xiàn)音頻播放的功能,供大家學習參考,希望對你有所幫助
    2022-03-03
  • 跟老齊學Python之讓人歡喜讓人憂的迭代

    跟老齊學Python之讓人歡喜讓人憂的迭代

    跟一些比較牛X的程序員交流,經(jīng)常聽到他們嘴里冒出一個不標準的英文單詞,而loop、iterate、traversal和recursion如果不在其內(nèi),總覺得他還不夠牛X。當讓,真正牛X的絕對不會這么說的,他們只是說“循環(huán)、迭代、遍歷、遞歸”,然后再問“這個你懂嗎?”。
    2014-10-10
  • Django模板語法、請求與響應的案例詳解

    Django模板語法、請求與響應的案例詳解

    本文主要介紹了Django的模板語法、請求與響應,包括如何創(chuàng)建和渲染模板文件、傳參機制、靜態(tài)文件的引入以及如何處理GET和POST請求,通過綜合小案例,展示了如何使用Django實現(xiàn)一個簡單的登錄頁面并根據(jù)用戶名密碼進行驗證,感興趣的朋友跟隨小編一起看看
    2025-01-01

最新評論

乌海市| 花垣县| 大理市| 获嘉县| 垣曲县| 普安县| 陆丰市| 延吉市| 伊吾县| 漠河县| 冕宁县| 连州市| 会东县| 温泉县| 昌平区| 原阳县| 和静县| 本溪| 常宁市| 新田县| 弥渡县| 大新县| 罗定市| 通河县| 高邮市| 彰化县| 探索| 攀枝花市| 古浪县| 白沙| 德钦县| 乌兰县| 微山县| 类乌齐县| 远安县| 犍为县| 务川| 新泰市| 桑日县| 商城县| 陆丰市|