0

enter image description here

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?

user1592380
  • 34,265
  • 92
  • 284
  • 515

1 Answers1

1

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.

Community
  • 1
  • 1
Ryan O'Donnell
  • 617
  • 6
  • 14
  • Thanks for your response Ryan I've created a new db 'flask' .I looked at the link and am trying to follow this – user1592380 Jan 27 '16 at 20:51
  • Ryan, would you mind looking at http://stackoverflow.com/questions/35047914/flask-sqlalchemy-exc-programmingerror-psycopg2-programmingerror-relation-u/35048052?noredirect=1#comment57821230_35048052 , I'm having trouble auto creating the table – user1592380 Jan 27 '16 at 22:12