国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

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

瀏覽:2日期:2022-06-16 18:16:03
目錄應用場景福音快快使用模型類效果注意事項

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

應用場景

1、后臺開發中,需要經常對數據庫進行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_dbsqlacodegen mysql+oursql://user:password@localhost/dbnamesqlacodegen 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 --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的默認的postgres數據庫中有個這樣的表:

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

sqlacodegen postgresql://ridingroad:[email protected]:5432/postgres --outfile=models.py --tables friends模型類效果

查看輸出到models.py的內容

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

如果你有很多表,就直接指定數據庫唄(這是會生成整個數據庫的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);

再執行同一個命令:

sqlacodegen postgresql://ridingroad:[email protected]: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))

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

以上就是python用sqlacodegen根據已有數據庫(表)結構生成對應SQLAlchemy模型的詳細內容,更多關于python sqlacodegen的使用的資料請關注好吧啦網其它相關文章!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 国产原创在线视频 | 久久国产成人精品 | 成年女人毛片免费观看97 | 日本高清毛片视频在线看 | 欧美高清在线精品一区二区不卡 | 久香草视频在线观看 | 97视频在线观看免费播放 | 91成人午夜性a一级毛片 | 免费一级a毛片在线播放视 免费一级α片在线观看 | 韩国免费网站成人 | 91久久亚洲国产成人精品性色 | 成人黄色在线免费观看 | 粉嫩高中生的第一次在线观看 | 国产精品色内内在线播放 | 亚洲精品一区二区三区五区 | 一级特级欧美aaaaa毛片 | 成年人在线观看网站 | 亚欧美图片自偷自拍另类 | 久久亚洲国产精品 | 成年人午夜免费视频 | 成人在线免费播放 | 一级毛片免费不卡在线视频 | 熟女毛片| 欧美性猛交xxxx免费看手交 | 香港一级特黄高清免费 | fc2久久| 一本久 | 精品国产三级v | 亚洲成年男人的天堂网 | 欧美日韩一区二区三区视频在线观看 | 欧美性色黄大片一级毛片视频 | a免费毛片在线播放 | 亚洲男人的天堂久久无 | a级欧美片免费观看 | 最新中文字幕视频 | 久久爱青青草 | 狼人激情网 | 三级国产在线观看 | 国产黄色三级三级三级 | 久久久全国免费视频 | 九色porny真实丨国产18 |