-1
我的英語不太好,希望你能幫助我。 我似乎無法弄清楚爲什麼我對db.create_all()的調用不起作用。Flask SQLAlchemy db.create_all()無法在shell中創建數據庫
無法創建數據庫並且沒有錯誤。
>>python manager.py shell
>>from app import db
>>db.create_all()
目錄樹
CMDB/
├─app/
│ ├─auth/
│ ├─main/
│ ├─static/
│ ├─templates/
│ ├─__init__.py
│ ├─config.conf
│ ├─models.py
├─manager.py
__init__.py
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
def create_app():
app = Flask(__name__)
app.config.from_pyfile('config.conf')
db.init_app(app)
from .main import main as main_blueprint
from .auth import auth as auth_blueprint
app.register_blueprint(main_blueprint)
app.register_blueprint(auth_blueprint)
return app
config.conf
DEBUG = True
SECRET_KEY = 'test'
SQLALCHEMY_TRACK_MODIFICATIONS = True
SQLALCHEMY_COMMIT_ON_TEARDOWN = True
SQLALCHEMY_DATABASE_URI='mysql+pymysql://root:[email protected]:3306/cmdb'
models.py
from . import db
from flask_sqlalchemy import SQLAlchemy
from werkzeug.security import generate_password_hash,check_password_hash
from flask_security import RoleMixin, UserMixin
class Test(db.Model):
__tablename__ = 'test'
id = db.Column(db.Integer(), primary_key=True)
test = db.Column(db.String(80))
manager.py
from flask_script import Manager,Shell
from app import create_app,db
from flask_migrate import Migrate,MigrateCommand
app = create_app()
manager = Manager(app)
if __name__ == '__main__':
# app.run()
manager.run()
嘗試在其他文件中導入您的模型類。 – stamaimer