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?

3
  • and what the debugger says? Commented Jan 27, 2016 at 19:13
  • What is in your model files and db config? Eg.db = SQLAlchemy() Commented Jan 27, 2016 at 19:21
  • Sorry Guys , I made the changes above. Commented Jan 27, 2016 at 19:44

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your response Ryan I've created a new db 'flask' .I looked at the link and am trying to follow this
Ryan, would you mind looking at stackoverflow.com/questions/35047914/… , I'm having trouble auto creating the table

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.