最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[未解决]Flask中SQLAlchemy中更新数据库同时保持原有数据

database crifan 2440浏览 0评论

之前已经实现了Flask中通过SQLAlchemy中建立表,列,去操作数据库,保存了一些数据

现在需要更新数据库的模型,更新表中某些字段

但是希望保留之前的数据。

之前也听说过一些,升级数据库,且保留数据的技术和框架:

存储 | Flask之旅

中提到的:

Alembic

但是一直没去用。

Flask SQLAlchemy 升级数据库 保留数据

数据库 — flask mega-tutorial 1.0 documentation

Flask 数据库迁移工具 flask-migrate – 非梦的花朵 (Kevin QQ) – 博客频道 – CSDN.NET

python – 当SQLAlchemy修改了模型结构后,如何更新到已有数据表 – SegmentFault

然后去:

[已解决]SQLAlchemy的数据库迁移工具:Flask-Migrate vs Alembic

alembic 0.8.7 : Python Package Index

-》

zzzeek / alembic — Bitbucket

Welcome to Alembic’s documentation! — Alembic 0.8.8 documentation

Front Matter — Alembic 0.8.8 documentation

https://bitbucket.org/zzzeek/alembic/get/master.tar.gz

Tutorial — Alembic 0.8.8 documentation

-》作者的帖子里面,解释的很详细了:

Flask-Migrate: Alembic database migration wrapper for Flask – miguelgrinberg.com

以及:

Flask之更新表结构 – WF – 技术专题

去参考,去使用:

(SIPEvents) ➜  SIPEvents pip install Flask-Migrate
Collecting Flask-Migrate
  Downloading Flask-Migrate-2.0.0.tar.gz
Requirement already satisfied (use –upgrade to upgrade): Flask>=0.9 in /root/Envs/SIPEvents/lib/python2.7/site-packages (from Flask-Migrate)
Requirement already satisfied (use –upgrade to upgrade): Flask-SQLAlchemy>=1.0 in /root/Envs/SIPEvents/lib/python2.7/site-packages (from Flask-Migrate)
Collecting alembic>=0.6 (from Flask-Migrate)
  Downloading alembic-0.8.7.tar.gz (968kB)
    100% |████████████████████████████████| 972kB 128kB/s 
Collecting Flask-Script>=0.6 (from Flask-Migrate)
  Downloading Flask-Script-2.0.5.tar.gz (42kB)
    100% |████████████████████████████████| 51kB 165kB/s 
Requirement already satisfied (use –upgrade to upgrade): itsdangerous>=0.21 in /root/Envs/SIPEvents/lib/python2.7/site-packages (from Flask>=0.9->Flask-Migrate)
Requirement already satisfied (use –upgrade to upgrade): Jinja2>=2.4 in /root/Envs/SIPEvents/lib/python2.7/site-packages (from Flask>=0.9->Flask-Migrate)
Requirement already satisfied (use –upgrade to upgrade): Werkzeug>=0.7 in /root/Envs/SIPEvents/lib/python2.7/site-packages (from Flask>=0.9->Flask-Migrate)
Requirement already satisfied (use –upgrade to upgrade): click>=2.0 in /root/Envs/SIPEvents/lib/python2.7/site-packages (from Flask>=0.9->Flask-Migrate)
Requirement already satisfied (use –upgrade to upgrade): SQLAlchemy>=0.7 in /root/Envs/SIPEvents/lib/python2.7/site-packages (from Flask-SQLAlchemy>=1.0->Flask-Migrate)
Collecting Mako (from alembic>=0.6->Flask-Migrate)
  Downloading Mako-1.0.4.tar.gz (574kB)
    100% |████████████████████████████████| 583kB 108kB/s 
Collecting python-editor>=0.3 (from alembic>=0.6->Flask-Migrate)
  Downloading python-editor-1.0.1.tar.gz
Requirement already satisfied (use –upgrade to upgrade): MarkupSafe in /root/Envs/SIPEvents/lib/python2.7/site-packages (from Jinja2>=2.4->Flask>=0.9->Flask-Migrate)
Building wheels for collected packages: Flask-Migrate, alembic, Flask-Script, Mako, python-editor
  Running setup.py bdist_wheel for Flask-Migrate … done
  Stored in directory: /root/.cache/pip/wheels/8d/c7/42/dd3e625605b37b092d5471f172e8e525ea7d996afbb70c304d
  Running setup.py bdist_wheel for alembic … done
  Stored in directory: /root/.cache/pip/wheels/48/b0/eb/702f60b92527a913f024d250e437f3bcbbc4a24df618593bca
  Running setup.py bdist_wheel for Flask-Script … done
  Stored in directory: /root/.cache/pip/wheels/e2/ea/d8/8d114e46cef819f7d9879504a7f9cb2a88a479af2858223d9f
  Running setup.py bdist_wheel for Mako … done
  Stored in directory: /root/.cache/pip/wheels/2a/fa/9a/7030d96d3c51f433848e4cd67a353a411ba6deb5c14e150730
  Running setup.py bdist_wheel for python-editor … done
  Stored in directory: /root/.cache/pip/wheels/c2/49/ce/59657468e42b41eb86aa654650afdf5fe891822a0dc376eebe
Successfully built Flask-Migrate alembic Flask-Script Mako python-editor
Installing collected packages: Mako, python-editor, alembic, Flask-Script, Flask-Migrate
Successfully installed Flask-Migrate-2.0.0 Flask-Script-2.0.5 Mako-1.0.4 alembic-0.8.7 python-editor-1.0.1

装了:

  • Flask-Migrate-2.0.0

以及对应的依赖:

  • Flask-Script-2.0.5
  • Mako-1.0.4
  • alembic-0.8.7
  • python-editor-1.0.1

然后代码:

#!flask/bin/python
# -*- coding: UTF-8 -*-
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from sipevents import db
from sipevents import app
from sipevents import models
from sipevents import User, Event
# app = Flask(__name__)
# app.config[‘SQLALCHEMY_DATABASE_URI’] = ‘sqlite:///app.db’
# db = SQLAlchemy(app)
migrate = Migrate(app, db)
app.logger.debug(“type(migrate)=%s, migrate=%s”, type(migrate), migrate)
manager = Manager(app)
app.logger.debug(“type(manager)=%s, manager=%s”, type(manager), manager)
manager.add_command(‘db’, MigrateCommand)
app.logger.debug(“type(manager)=%s, manager=%s”, type(manager), manager)
if __name__ == ‘__main__’:
    manager.run()

运行结果:

(SIPEvents) ➜  SIPEvents python db_manager.py 

<div–<——————————————————————————

DEBUG in __init__ [/usr/share/nginx/html/SIPEvents/sipevents/__init__.py:21]:
images=<flask_images.core.Images object at 0x7fa6e23db4d0>

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in __init__ [/usr/share/nginx/html/SIPEvents/sipevents/__init__.py:24]:
db=<SQLAlchemy engine=’sqlite:////usr/share/nginx/html/SIPEvents/instance/sipevents.db’>

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in db_manager [db_manager.py:19]:
type(migrate)=<class ‘flask_migrate.Migrate’>, migrate=<flask_migrate.Migrate object at 0x7fa6e2601390>

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in db_manager [db_manager.py:22]:
type(manager)=<class ‘flask_script.Manager’>, manager=<flask_script.Manager object at 0x7fa6e0be0610>

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in db_manager [db_manager.py:24]:
type(manager)=<class ‘flask_script.Manager’>, manager=<flask_script.Manager object at 0x7fa6e0be0610>

<div–<——————————————————————————

usage: db_manager.py [-?] {shell,db,runserver} …
positional arguments:
  {shell,db,runserver}
    shell               Runs a Python shell inside Flask application context.
    db                  Perform database migrations
    runserver           Runs the Flask development server i.e. app.run()
optional arguments:
  -?, –help            show this help message and exit
(SIPEvents) ➜  SIPEvents python db_manager.py db init

<div–<——————————————————————————

DEBUG in __init__ [/usr/share/nginx/html/SIPEvents/sipevents/__init__.py:21]:
images=<flask_images.core.Images object at 0x7f0bad2704d0>

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in __init__ [/usr/share/nginx/html/SIPEvents/sipevents/__init__.py:24]:
db=<SQLAlchemy engine=’sqlite:////usr/share/nginx/html/SIPEvents/instance/sipevents.db’>

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in db_manager [db_manager.py:19]:
type(migrate)=<class ‘flask_migrate.Migrate’>, migrate=<flask_migrate.Migrate object at 0x7f0bad496390>

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in db_manager [db_manager.py:24]:
type(manager)=<class ‘flask_script.Manager’>, manager=<flask_script.Manager object at 0x7f0baba75610>

<div–<——————————————————————————

  Creating directory /usr/share/nginx/html/SIPEvents/migrations … done
  Creating directory /usr/share/nginx/html/SIPEvents/migrations/versions … done
  Generating /usr/share/nginx/html/SIPEvents/migrations/README … done
  Generating /usr/share/nginx/html/SIPEvents/migrations/env.py … done
  Generating /usr/share/nginx/html/SIPEvents/migrations/env.pyc … done
  Generating /usr/share/nginx/html/SIPEvents/migrations/alembic.ini … done
  Generating /usr/share/nginx/html/SIPEvents/migrations/script.py.mako … done
  Please edit configuration/connection/logging settings in ‘/usr/share/nginx/html/SIPEvents/migrations/alembic.ini’ before proceeding.

去看了看对应的文件:

(SIPEvents) ➜  SIPEvents ll migrations 
total 24K
-rw-r–r– 1 root root  770 Aug 29 18:04 alembic.ini
-rwxr-xr-x 1 root root 2.9K Aug 29 18:04 env.py
-rw-r–r– 1 root root 2.7K Aug 29 18:04 env.pyc
-rwxr-xr-x 1 root root   38 Aug 29 18:04 README
-rwxr-xr-x 1 root root  412 Aug 29 18:04 script.py.mako
drwxr-xr-x 2 root root 4.0K Aug 29 18:04 versions
(SIPEvents) ➜  SIPEvents ll migrations/versions 
total 0
(SIPEvents) ➜  SIPEvents cat migrations/alembic.ini 
# A generic, single database configuration.
[alembic]
# template used to generate migration files
# file_template = %%(rev)s_%%(slug)s
# set to ‘true’ to run the environment during
# the ‘revision’ command, regardless of autogenerate
# revision_environment = false
# Logging configuration
[loggers]
keys = root,sqlalchemy,alembic
[handlers]
keys = console
[formatters]
keys = generic
[logger_root]
level = WARN
handlers = console
qualname =
[logger_sqlalchemy]
level = WARN
handlers =
qualname = sqlalchemy.engine
[logger_alembic]
level = INFO
handlers =
qualname = alembic
[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic
[formatter_generic]
format = %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %H:%M:%S
(SIPEvents) ➜  SIPEvents python db_manager.py db migrate

<div–<——————————————————————————

DEBUG in __init__ [/usr/share/nginx/html/SIPEvents/sipevents/__init__.py:21]:
images=<flask_images.core.Images object at 0x7fc7f52684d0>

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in __init__ [/usr/share/nginx/html/SIPEvents/sipevents/__init__.py:24]:
db=<SQLAlchemy engine=’sqlite:////usr/share/nginx/html/SIPEvents/instance/sipevents.db’>

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in db_manager [db_manager.py:19]:
type(migrate)=<class ‘flask_migrate.Migrate’>, migrate=<flask_migrate.Migrate object at 0x7fc7f548e390>

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in db_manager [db_manager.py:22]:
type(manager)=<class ‘flask_script.Manager’>, manager=<flask_script.Manager object at 0x7fc7f3a6d610>

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in db_manager [db_manager.py:24]:
type(manager)=<class ‘flask_script.Manager’>, manager=<flask_script.Manager object at 0x7fc7f3a6d610>

<div–<——————————————————————————

INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.env] No changes in schema detected.

去更改:

/Users/crifan/dev/dev_root/daryun/SIPEvents/sourcecode/flask/sipevents/models.py

添加:notification_time

转载请注明:在路上 » [未解决]Flask中SQLAlchemy中更新数据库同时保持原有数据

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.223 seconds, using 22.21MB memory