婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > python用sqlacodegen根據已有數據庫(表)結構生成對應SQLAlchemy模型

python用sqlacodegen根據已有數據庫(表)結構生成對應SQLAlchemy模型

熱門標簽:海外網吧地圖標注注冊 孝感營銷電話機器人效果怎么樣 地圖標注自己和別人標注區(qū)別 騰訊地圖標注沒法顯示 商家地圖標注海報 打電話機器人營銷 ai電銷機器人的優(yōu)勢 南陽打電話機器人 聊城語音外呼系統(tǒng)

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

應用場景

1、后臺開發(fā)中,需要經常對數據庫進行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);

再執(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根據已有數據庫(表)結構生成對應SQLAlchemy模型的詳細內容,更多關于python sqlacodegen的使用的資料請關注腳本之家其它相關文章!

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

標簽:撫州 南寧 牡丹江 聊城 六盤水 揚州 楊凌 迪慶

巨人網絡通訊聲明:本文標題《python用sqlacodegen根據已有數據庫(表)結構生成對應SQLAlchemy模型》,本文關鍵詞  python,用,sqlacodegen,根據,;如發(fā)現(xiàn)本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《python用sqlacodegen根據已有數據庫(表)結構生成對應SQLAlchemy模型》相關的同類信息!
  • 本頁收集關于python用sqlacodegen根據已有數據庫(表)結構生成對應SQLAlchemy模型的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 闽侯县| 犍为县| 双流县| 河津市| 扶余县| 荣成市| 潜山县| 商都县| 云霄县| 永嘉县| 浮梁县| 济源市| 杭州市| 南平市| 大新县| 阿鲁科尔沁旗| 美姑县| 苏州市| 米易县| 南宁市| 喀喇沁旗| 公主岭市| 旌德县| 微山县| 肃宁县| 阿荣旗| 留坝县| 名山县| 安福县| 石棉县| 大石桥市| 百色市| 巴青县| 栖霞市| 兴海县| 临澧县| 宁南县| 合川市| 神农架林区| 绥滨县| 承德市|