0

django之数据库操作ORM

 1 year ago
source link: https://blog.51cto.com/u_12131824/5688222
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

django之数据库操作ORM

精选 原创

细哥细妹 2022-09-19 10:46:44 ©著作权

文章标签 数据库 python mysql 文章分类 Python 编程语言 阅读数196

使用 ​ ​models.py​​ 生成对应的表结构

  • makemigrations:根据检测到的模型创建新的迁移,自动生成python语句,如:​​migrations/0001_initial.py​​。
  • 这个命令只是生成python文件,不会作用到数据库中
  • migrate:执行刚生成的python脚本,在数据库中生成对应 表/字段 ,同时记录到 django_migrations 表中
  • 1.删除之前的数据库,并把没有变动的​​主机表​​和​​用户​​表增加到新数据库中
mysqldump -uroot -p123456 aoshuapi > /tmp/aoshuapi_20220914.sql
mysql -uroot -p123456 -e "drop database aoshuapi;"
mysql -uroot -p123456 -e "create database aoshuapi;
# 在jenkins上发布
# 将备份数据库的 gamedata_assetzone auth_user 插入新数据库中
cp -ar /opt/hefu/config/hefu.conf /application/aoshuapi/merge/hefu/config/
  • 2.重新生成django数据库表结构
python3 manage.py makemigrations
python3 manage.py migrate

可以保证每次的数据库变更都是生效的,但是删除数据库,需要停服才能进行操作。并且当数据量过大时,停服时间会变得很长

因为makemigrations生成了对应的python脚本,进而在数据库生成对应数据库操作;我们可以直接通过修改python文件及操作数据库来完成操作即可


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK