色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術(shù)文章
文章詳情頁

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

瀏覽:4日期:2022-06-16 18:16:03
目錄應(yīng)用場景福音快快使用模型類效果注意事項(xiàng)

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

應(yīng)用場景

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

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

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

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

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

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

福音

還是那句話,Python大法好。 這里就介紹一個(gè)根據(jù)已有數(shù)據(jù)庫(表)結(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快快使用

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

sqlacodegen postgresql:///some_local_dbsqlacodegen mysql+oursql://user:password@localhost/dbnamesqlacodegen 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 --noindexesignore indexes --noconstraints ignore constraints --nojoined don’t autodetect joined table inheritance --noinflectdon’t try to convert tables names to singular form --noclassesdon’t generate classes, only tables --outfile OUTFILE file to write output to (default: stdout)

目前我在postgresql的默認(rèn)的postgres數(shù)據(jù)庫中有個(gè)這樣的表:

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

為了使用ORM進(jìn)行操作,我需要獲取它的modle類但唯一索引的model類怎么寫呢? 我們借助sqlacodegen來自動(dòng)生成就好了

sqlacodegen postgresql://ridingroad:ridingroad@127.0.0.1:5432/postgres --outfile=models.py --tables friends模型類效果

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

# coding: utf-8from sqlalchemy import Column, Index, Stringfrom sqlalchemy.ext.declarative import declarative_baseBase = declarative_base()metadata = Base.metadataclass 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ù)庫唄(這是會(huì)生成整個(gè)數(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或這張表是有兩個(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-8from sqlalchemy import Column, Index, MetaData, String, Tablemetadata = 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ù)庫(表)結(jié)構(gòu)生成對(duì)應(yīng)SQLAlchemy模型的詳細(xì)內(nèi)容,更多關(guān)于python sqlacodegen的使用的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 欧美成人 一区二区三区 | 成人免费午夜视频 | 亚洲国产高清人在线 | 国产精品单位女同事在线 | 免费a级黄毛片 | 在线观看精品视频一区二区三区 | 80岁色老头69av | 欧美日韩在线播一区二区三区 | 精品国产一区二区三区不卡在线 | 美女超爽久久久久网站 | 亚洲午夜国产精品 | 一a一片一级一片啪啪 | 欧美一级毛片片免费孕妇 | 欧美一级aa天码毛片 | 男人天堂社区 | 日本韩国一级片 | 毛片网站观看 | 久久国产精品久久精 | 欧美成人观看视频在线 | 毛片免费观看视频 | 欧美顶级毛片在线播放小说 | 欧美久色 | 黄色三级国产 | 99视频在线精品 | 国内精品久久影视 | 国产成人精品免费 | freese×video性欧美丝袜 | 日本精品夜色视频一区二区 | 国产大片在线观看 | 日韩免费一级a毛片在线播放一级 | 欧美国产日韩在线播放 | 日韩视频观看 | 国产只有精品 | 欧美特级大片 | 国产日本亚洲欧美 | 免费观看成人毛片 | 成人午夜免费视频 | 国产一区不卡 | www亚洲成人 | 国产精品成人一区二区不卡 | 国产五区 |