python自动生成model文件过程详解
作者:大步向前blue 时间:2023-09-30 02:54:05
生成方式
Python中想要自动生成 model文件可以通过 sqlacodegen这个命令来生成对应的model文件
sqlacodegen 你可以通过pip去安装:
pip install sqlacodegen
格式:
sqlacodegen mysql+pymysql://username:password@host/database_name > model.py
说明:
mysql+pymysql : 表示连接数据库的连接方式
username : 连接MySQL数据库的用户名
password : 连接MySQL数据库用户对应的密码
host : 数据库的主机地址
database_name : 需要生成model的数据库名【一定是数据库名】
问题: 如果只想生成数据库中指定表的model文件怎么办?
答案就是:
给 sqlacodegen 加一个 --table 的参数即可
案例:
👉⚡️sqlacodegen --tables products mysql+pymysql://root:root@127.0.0.1/shopify > products.py
👉⚡️ls
products.py
结果:
👉⚡️cat products.py
# coding: utf-8
from sqlalchemy import CHAR, Column, String, Text, text
from sqlalchemy.dialects.mysql import INTEGER
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
metadata = Base.metadata
class Product(Base):
__tablename__ = 'products'
id = Column(INTEGER(16), primary_key=True)
title = Column(String(256), nullable=False, server_default=text("''"))
product_id = Column(INTEGER(16))
shop_url = Column(String(120))
body_html = Column(Text)
vendor = Column(String(64))
product_type = Column(String(64))
created_at = Column(CHAR(30))
updated_at = Column(CHAR(30))
handle = Column(String(256))
published_at = Column(CHAR(30))
template_suffix = Column(String(256))
tags = Column(String(256))
published_scope = Column(CHAR(10), nullable=False, server_default=text("'web'"))
👉⚡️
来源:https://www.cnblogs.com/yinguohai/p/11778488.html
标签:python,生成,model,文件
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
浅析MySQL并行复制
2024-01-13 02:54:10
![](https://img.aspxhome.com/file/2023/2/72502_0s.png)
使用vue.js写一个tab选项卡效果
2024-04-22 22:23:34
![](https://img.aspxhome.com/file/2023/6/135806_0s.jpg)
Oracle中在pl/sql developer修改表的2种方法
2024-01-20 13:06:35
在MySQL中获得更好的全文搜索结果
2008-05-09 10:38:00
PHP实现的redis主从数据库状态检测功能示例
2023-09-08 01:36:38
python 3利用Dlib 19.7实现摄像头人脸检测特征点标定
2022-07-31 05:54:21
![](https://img.aspxhome.com/file/2023/3/76033_0s.gif)
Chrome和firefox使用比较测评
2010-03-26 12:13:00
![](https://img.aspxhome.com/file/UploadPic/20103/26/2010326124036780s.gif)
Python绘图库Matplotlib的基本用法
2023-04-27 15:44:34
![](https://img.aspxhome.com/file/2023/8/86568_0s.png)
Python序列化模块JSON与Pickle
2022-11-06 00:24:37
数据库性能优化之冗余字段的作用
2011-03-03 19:21:00
Python 格式化输出_String Formatting_控制小数点位数的实例详解
2023-11-29 12:09:14
JavaScript对象属性操作实例解析
2024-05-13 09:18:47
![](https://img.aspxhome.com/file/2023/3/125933_0s.png)
Python turtle画图库&&画姓名实例
2021-07-03 09:02:02
![](https://img.aspxhome.com/file/2023/8/109868_0s.jpg)
opencv实现答题卡识别
2021-02-28 12:40:05
![](https://img.aspxhome.com/file/2023/5/102845_0s.jpg)
Python探索之Metaclass初步了解
2023-12-22 10:25:37
Python插件机制实现详解
2021-08-28 06:55:30
![](https://img.aspxhome.com/file/2023/2/104832_0s.jpg)
教你利用PyTorch实现sin函数模拟
2021-06-23 18:17:25
Go语言基础函数基本用法及示例详解
2024-05-09 14:57:15
![](https://img.aspxhome.com/file/2023/7/127067_0s.png)
python将ip地址转换成整数的方法
2022-10-17 23:40:23
MySQL一键安装Shell脚本的实现
2024-01-16 23:28:29