我正在尝试基于SQLite db在Flask api上进行迁移。这是我的项目结构
├── app.py
├── blueprints
├── conf.py
├── db.sqlite
├── __init__.py
├── migrations
├── models.py
└── templates
然后我在app.py
from flask import Flask, jsonify, request, render_template
from flask_sqlalchemy import SQLAlchemy
from conf import ErrorResponses, SuccessResponses
# initialization
app = Flask(__name__)
db = SQLAlchemy(app)
from models import User, db, BlackListToken
# extensions
from flask_migrate import Migrate
mig = Migrate(app, db)
但是在flask db migrate -m "rm id and made name/surname nullable"
期间从自定义模块导入失败。
Usage: flask db migrate [OPTIONS]
Error: While importing "api_flask.app", an ImportError was raised:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/flask/cli.py", line 235, in locate_app
__import__(module_name)
File "/home/zahessi/all/bubbie_D/api_flask/app.py", line 9, in <module>
from conf import ErrorResponses, SuccessResponses
ModuleNotFoundError: No module named 'conf'
models.py
的外观如下:
from app import db
class User(db.Model):
__tablename__ = 'users'
username = db.Column(db.String(32), index=True, unique=True, nullable=False)
email = db.Column(db.String(64), unique=True, nullable=False)
password_hash = db.Column(db.String(64), nullable=False)
name = db.Column(db.String(32))
surname = db.Column(db.String(32))
public_id = db.Column(db.String(32))
creation_date = db.Column(db.DateTime)
...
当我从自定义py文件中删除所有导入时,所有内容似乎都可以使用,但是,这是很多不必要的工作。您有什么建议为什么它会以这种方式工作?
UPD:添加了conf.py
。根据导入的顺序,此操作在每个本地导入以及蓝图上都会失败。
from re import findall
class ErrorResponses:
usernameOrPasswordBlank = {
"success": False,
"error": "Username and password cannot be blank."
}
userAlreadyExists = {"success": False, "error": "Such user already exists"}
incorrectData = {
"success": False,
"error": "Incorrect username or password"
}
invalidData = {
"success": False,
"error": "Username or password is invalid"
}
noUser = {
"success": False,
"error": "There is no user with such username or public_id"
}
...
答案 0 :(得分:1)
我看到您已经解决了问题,但是我想向您展示比安装flask-script
和创建manage.py
文件更简单的解决方案。您只需要删除与app.py文件处于同一级别的__init__.py
文件。它为我解决了问题。干杯:)
答案 1 :(得分:0)
要解决您的问题,我建议您使用flask_script而不是flask cli。只需在您的根目录中创建manage.py文件,然后执行以下操作:
struct Node{
int key;
Node *left, *right;
};
int NumNodes = 0;
const int SIZE = 100;
Node data[SIZE];
Node* newNode(int value){
if(NumNodes == SIZE) {
cout << "This Node Pool is full." << endl;
exit(1);
}
Node* temp = &data[NumNodes++];
temp->key = value;
temp->left = temp->right = NULL;
return temp;
}
要启动迁移,只需执行以下操作:
from flask_migrate import Migrate, MigrateCommand
from flask_script import Manager
migrate = Migrate(app, db)
manager = Manager(app)
manager.add_command('db', MigrateCommand)
或python manage.py db migrate
(取决于您的PATH配置)