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

【已解决】如何运行Flask-Migrate的db init

Flask crifan 5899浏览 0评论

折腾:

【未解决】Flask中管理数据库:升级数据库

期间,对于自己这里的flask程序,结构是带了子文件夹的:

所以没法直接像很多帖子:

Welcome to Flask-Migrate’s documentation! — Flask-Migrate documentation

Flask-SQLAlchemy 和 Flask-Migrate 使用

Flask-Migrate实现数据库迁移 | LinuxHub

中说的:

直接 用

flask db init
app.py db init
python app.py db init

所以去试了几种,结果都不行:

python run.py db init

(RunningFast) ➜  RunningFast python run.py db init

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

DEBUG in __init__ [/root/RunningFast/runningfast/__init__.py:42]:
redis_store=<flask_redis.FlaskRedis object at 0x7f232f7c0fd0>

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

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

DEBUG in __init__ [/root/RunningFast/runningfast/__init__.py:45]:
db=<SQLAlchemy engine=’mysql://runningfast:Jiandao123@localhost/runningfast_dev’>

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

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

DEBUG in __init__ [/root/RunningFast/runningfast/__init__.py:48]:
migrate=<flask_migrate.Migrate object at 0x7f232db1c1d0>

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

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

DEBUG in models [/root/RunningFast/runningfast/models.py:13]:
prefix=user-, generatedUuid4Str=e6c54688-41ab-4c6e-a577-ef5986bd42ce, newUuid=user-e6c54688-41ab-4c6e-a577-ef5986bd42ce

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

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

DEBUG in views [/root/RunningFast/runningfast/views.py:21]:
API_VERSION=1.0, API_URL_PREFIX=/runningfast/api/v1.0, OPEN_API_URL_PREFIX=/runningfast/api/v1.0/open

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

 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat

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

DEBUG in __init__ [/root/RunningFast/runningfast/__init__.py:42]:
redis_store=<flask_redis.FlaskRedis object at 0x7f21a43adfd0>

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

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

DEBUG in __init__ [/root/RunningFast/runningfast/__init__.py:45]:
db=<SQLAlchemy engine=’mysql://runningfast:Jiandao123@localhost/runningfast_dev’>

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

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

DEBUG in __init__ [/root/RunningFast/runningfast/__init__.py:48]:
migrate=<flask_migrate.Migrate object at 0x7f21a27091d0>

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

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

DEBUG in models [/root/RunningFast/runningfast/models.py:13]:
prefix=user-, generatedUuid4Str=30d387b2-1d7f-4dd9-867d-4473d9521678, newUuid=user-30d387b2-1d7f-4dd9-867d-4473d9521678

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

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

DEBUG in views [/root/RunningFast/runningfast/views.py:21]:
API_VERSION=1.0, API_URL_PREFIX=/runningfast/api/v1.0, OPEN_API_URL_PREFIX=/runningfast/api/v1.0/open

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

 * Debugger is active!
 * Debugger pin code: 218-104-482
^C#                                 

结果试了另外几种,都不行:

(RunningFast) ➜  RunningFast python runningfast.py db init
python: can’t open file ‘runningfast.py’: [Errno 2] No such file or directory
(RunningFast) ➜  RunningFast python runningfast/__init__.py db init
Traceback (most recent call last):
  File "runningfast/__init__.py", line 18, in <module>
    app.config.from_object(‘config’) # load config.py
  File "/root/Envs/RunningFast/lib/python2.7/site-packages/flask/config.py", line 163, in from_object
    obj = import_string(obj)
  File "/root/Envs/RunningFast/lib/python2.7/site-packages/werkzeug/utils.py", line 443, in import_string
    sys.exc_info()[2])
  File "/root/Envs/RunningFast/lib/python2.7/site-packages/werkzeug/utils.py", line 418, in import_string
    __import__(import_name)
werkzeug.utils.ImportStringError: import_string() failed for ‘config’. Possible reasons are:
– missing __init__.py in a package;
– package or module path not included in sys.path;
– duplicated package or module name taking precedence in sys.path;
– missing module, class, function or variable;
Debugged import:
– ‘config’ not found.
Original exception:
ImportError: No module named config
(RunningFast) ➜  RunningFast flask db init
Usage: flask db init [OPTIONS]
Error: Could not locate Flask application. You did not provide the FLASK_APP environment variable.
For more information see http://flask.pocoo.org/docs/latest/quickstart/

flask-migrate how do db init

flask-migrate how run db init

flask-migrate how init

Flask by Example – Setting up Postgres, SQLAlchemy, and Alembic – Real Python

python – flask-migrate doesn’t detect models – Stack Overflow

Flask学习记录之Flask-Migrate – agmcs – 博客园

-》好像是,需要单独写一个py文件,比如叫做:

db_manager.py

#!flask/bin/python
# -*- coding: UTF-8 -*-
from runningfast import db, app, gLog
from flask_migrate import Migrate
migrate = Migrate(app, db)
gLog.debug("migrate=%s", migrate)

 

但是去运行,结果没有生成migrations文件夹。

(RunningFast) ➜  RunningFast python db_manager.py db init

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

DEBUG in __init__ [/root/RunningFast/runningfast/__init__.py:40]:
redis_store=<flask_redis.FlaskRedis object at 0x7f9bc768cf90>

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

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

DEBUG in __init__ [/root/RunningFast/runningfast/__init__.py:43]:
db=<SQLAlchemy engine=’mysql://runningfast:Jiandao123@localhost/runningfast_dev’>

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

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

DEBUG in models [/root/RunningFast/runningfast/models.py:13]:
prefix=user-, generatedUuid4Str=3e1456be-fd62-4dc5-9717-4ab3e2667d16, newUuid=user-3e1456be-fd62-4dc5-9717-4ab3e2667d16

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

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

DEBUG in views [/root/RunningFast/runningfast/views.py:21]:
API_VERSION=1.0, API_URL_PREFIX=/runningfast/api/v1.0, OPEN_API_URL_PREFIX=/runningfast/api/v1.0/open

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

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

DEBUG in db_manager [db_manager.py:9]:
migrate=<flask_migrate.Migrate object at 0x7f9bc5982710>

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

(RunningFast) ➜  RunningFast ll
total 56K
-rw-r–r– 1 root root  527 Sep 29 15:03 config.py
-rw-r–r– 1 root root  349 Oct  8 10:58 config.pyc
-rw-r–r– 1 root root 2.3K Oct  9 17:31 db_create.py
-rw-r–r– 1 root root  179 Oct 10 17:54 db_manager.py
drwxr-xr-x 2 root root 4.0K Oct  8 10:57 instance
drwxr-xr-x 2 root root 4.0K Oct  8 10:58 logs
-rw-r–r– 1 root root  675 Sep 29 21:30 README.md
-rw-r–r– 1 root root  342 Oct 10 12:04 requirements.txt
drwxr-xr-x 2 root root 4.0K Oct 10 17:54 runningfast
-rw-r–r– 1 root root  137 Sep 29 15:06 run.py
-rw-r–r– 1 root root  229 Oct  8 10:58 run.pyc
-rw-r–r– 1 root root 8.8K Sep 29 21:59 supervisor.conf

【总结】

然后最后用:

db_manager.py

#!flask/bin/python
# -*- coding: UTF-8 -*-
from runningfast import db, app, gLog
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
migrate = Migrate(app, db)
gLog.debug("migrate=%s", migrate)
manager = Manager(app)
gLog.debug("manager=%s", manager)
manager.add_command(‘db’, MigrateCommand)
if __name__ == ‘__main__’:
    manager.run()

然后去运行,可以生成migrations文件夹了:

(RunningFast) ➜  RunningFast python db_manager.py db init

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

DEBUG in __init__ [/root/RunningFast/runningfast/__init__.py:40]:
redis_store=<flask_redis.FlaskRedis object at 0x7f3fde3dcf90>

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

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

DEBUG in __init__ [/root/RunningFast/runningfast/__init__.py:43]:
db=<SQLAlchemy engine=’mysql://runningfast:Jiandao123@localhost/runningfast_dev’>

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

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

DEBUG in models [/root/RunningFast/runningfast/models.py:13]:
prefix=user-, generatedUuid4Str=27b65472-7611-43f9-ac21-f87d3b2d2801, newUuid=user-27b65472-7611-43f9-ac21-f87d3b2d2801

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

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

DEBUG in views [/root/RunningFast/runningfast/views.py:21]:
API_VERSION=1.0, API_URL_PREFIX=/runningfast/api/v1.0, OPEN_API_URL_PREFIX=/runningfast/api/v1.0/open

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

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

DEBUG in db_manager [db_manager.py:11]:
migrate=<flask_migrate.Migrate object at 0x7f3fdc6d2710>

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

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

DEBUG in db_manager [db_manager.py:14]:
manager=<flask_script.Manager object at 0x7f3fdc6d2790>

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

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

转载请注明:在路上 » 【已解决】如何运行Flask-Migrate的db init

发表我的评论
取消评论

表情

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
89 queries in 0.177 seconds, using 22.25MB memory