Reputation: 36317
I am working on a flask app based on http://code.tutsplus.com/tutorials/intro-to-flask-signing-in-and-out--net-29982.
As part of the tut I'm trying to connect to a postgres server, with a structure as in the screenshot. I've added a table 'yournewdb' which you can see.
Based on the tut I have the following code in my main file ('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)
When I go to http://127.0.0.1:5000/testdb I'm getting an internal server error. The debugger gives:
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)})
What am I doing wrong?
Upvotes: 0
Views: 669
Reputation: 617
Seems you're connecting to a table, not a DB, correct? Why don't you change yournewdb
to postgres
or make a new DB? You'll still have to make your table. You can have SQLAlchemy do this for you. Here is a great answer on that: https://stackoverflow.com/a/20749534/2326132
I'd suggest making a new database for each project, even if they are all test projects. You'll run into fewer issues.
Upvotes: 1