我正在开发基于http://code.tutsplus.com/tutorials/intro-to-flask-signing-in-and-out--net-29982的烧瓶应用程序。
作为Tut的一部分,我试图连接到postgres服务器,其结构如截图所示。我已经添加了一张表格&your 39; yournewdb'你可以看到。
根据tut,我的主文件中包含以下代码(' routes.py'):
app.config['SQLALCHEMY_DATABASE_URI'] = "postgresql://postgres:123@localhost/yournewdb"
from models import db
db.init_app(app)
@app.route('/testdb')
def testdb():
if db.session.query("1").from_statement("SELECT 1").all():
return 'It works.'
else:
return 'Something is broken.'
models.py:
from flask.ext.sqlalchemy import SQLAlchemy
from werkzeug import generate_password_hash, check_password_hash
db = SQLAlchemy()
class User(db.Model):
__tablename__ = 'users'
uid = db.Column(db.Integer, primary_key = True)
firstname = db.Column(db.String(100))
lastname = db.Column(db.String(100))
email = db.Column(db.String(120), unique=True)
pwdhash = db.Column(db.String(54))
def __init__(self, firstname, lastname, email, password):
self.firstname = firstname.title()
self.lastname = lastname.title()
self.email = email.lower()
self.set_password(password)
def set_password(self, password):
self.pwdhash = generate_password_hash(password)
def check_password(self, password):
return check_password_hash(self.pwdhash, password)
当我转到http://127.0.0.1:5000/testdb时,我收到内部服务器错误。调试器给出:
C:\envs\virtalenvs\flask_mini\lib\site-packages\flask_sqlalchemy\__init__.py:800: UserWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True to suppress this warning.
warnings.warn('SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True to suppress this warning.')
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
C:\envs\virtalenvs\flask_mini\lib\site-packages\sqlalchemy\sql\elements.py:3779: SAWarning: Textual SQL expression 'SELECT 1' should be explicitly declared as text('SELECT 1') (this warning may be suppressed after 10 occurrences)
{"expr": util.ellipses_string(element)})
我做错了什么?
答案 0 :(得分:1)
似乎你连接到桌子而不是数据库,对吗?为什么不将yournewdb
更改为postgres
或创建新的数据库?你还是要做你的桌子。您可以让SQLAlchemy为您执行此操作。这是一个很好的答案:https://stackoverflow.com/a/20749534/2326132
我建议为每个项目创建一个新数据库,即使它们都是测试项目。你会遇到更少的问题。