我尝试使用postgresql启动我的烧瓶应用程序但是得到的错误似乎无法创建表我认为我做的所有配置都正确但现在只是得到这个错误我尝试使用不同的角色和不同的数据库但是没有工作所以这是错误
PS:它工作得很好但是我意外地将数据库放在postgresql中并且不得不创建新的sqlalchemy.exc.ProgrammingError
ProgrammingError: (ProgrammingError) column users.id does not exist LINE 1: SELECT users.id AS users_id, users.name AS users_name, users... ^ 'SELECT users.id AS users_id, users.name AS users_name, users.age AS users_age, users.email AS users_email, users.bestfriend_id AS users_bestfriend_id \nFROM users \nWHERE users.name = %(name_1)s \n LIMIT %(param_1)s' {'name_1': u'orchid', 'param_1': 1}
config.py:
import os
CSRF_ENABLE = True
basedir = os.path.abspath(os.path.dirname(__file__))
MONKEYS_PAGE = 2
class Config(object):
DEBUG = False
SECRET_KEY = 'Thisismysecretkey'
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL','postgresql+psycopg2://someuser:somepassword@localhost/myapp')
print SQLALCHEMY_DATABASE_URI
__ init __.py:
db = SQLAlchemy()
lm = LoginManager()
lm.login_view = 'login'
login_manager = LoginManager()
login_manager.login_view = 'layout.login'
def create_app(config_name):
app = Flask(__name__)
app.config['DEBUG'] = True
app.config.from_object(config[config_name])
db.init_app(app)
login_manager.init_app(app)
# login_manager.user_loader(load_user)
from .layout import layout as appr_blueprint
# register our blueprints
app.register_blueprint(appr_blueprint)
from .auth import auth as auth_blueprint
app.register_blueprint(auth_blueprint)
return app
manage.py:
import os
import unittest
import coverage
from flask.ext.migrate import Migrate, MigrateCommand
from app import create_app
from flask.ext.script import Manager
from app import db
from app.model import Users
app = create_app(os.environ['FLASK_CONFIG'] or 'default')
magrate = Migrate(app, db)
manager = Manager(app)
manager.add_command('db', MigrateCommand)
@manager.command
def init_db():
db.drop_all()
db.create_all()
@manager.command
def test():
from subprocess import call
call(['nosetests', '-v',
'--with-coverage', '--cover-package=app', '--cover-branches',
'--cover-erase', '--cover-html', '--cover-html-dir=cover'])
if __name__ == '__main__':
manager.run()
我不确定是否需要此文件? 当我运行db_create.py时会收到错误
raise RuntimeError('application not registered on db '
RuntimeError: application not registered on db instance and no application bound to current context
db_create.py:
from app import db,model
db.create_all()
db.session.commit(
)
答案 0 :(得分:0)
db_create.py
不是您编写的管理脚本的一部分。因此,它不是创建应用程序并激活应用程序上下文。
from app import create_app, db
with create_app(os.environ['FLASK_CONFIG'] or 'default').app_context():
db.create_all()
但是,如果您使用的是Alembic(在本例中为Flask-Migrate),请勿使用db.create_all
。您应该只使用迁移来设置数据库:python manage.py db upgrade
。