python - Flask : sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) relation "users" does not exist -
i working on flask app based on http://code.tutsplus.com/tutorials/intro-to-flask-signing-in-and-out--net-29982.
as part of tut i'm trying connect postgres server, structure in screenshot. i've added db 'flask' can see.
based on tut have following code in main file ('routes.py'):
from flask.ext.sqlalchemy import sqlalchemy flask import flask app = flask(__name__) app.config['sqlalchemy_database_uri'] = "postgresql://postgres:123@localhost/flask" db = sqlalchemy(app) models import user # db.init_app(app) db.create_all() db.session.commit() admin = user('admin', 'admin@example.com', 'admin1', 'admin1@example.com') guest = user('admi2', 'admin@ex1ample.com', 'admin', 'admin2@example.com') # guest = user('guest', 'guest@example.com') db.session.add(admin) db.session.add(guest) db.session.commit() models.py:
from flask.ext.sqlalchemy import sqlalchemy 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 run debugger gives:
sqlalchemy.exc.programmingerror: (psycopg2.programmingerror) relation "users" not exist line 1: insert users (firstname, lastname, email, pwdhash) valu... ^ [sql: 'insert users (firstname, lastname, email, pwdhash) values (%(firstname)s, %(lastname)s, %(email)s, %(pwdhash)s) returning users.uid'] [parameters: {'lastname': 'admin@example.com', 'firstname': 'admin', 'pwdhash': 'pbkdf2:sha1:1000$ezvjnkho$64f59c34364e3d6094d126fa3ca2b327ab39e302', 'email': 'admin1'}] what doing wrong?
you're initializing database twice.
i'd suggest taking @ this: http://flask.pocoo.org/docs/0.10/patterns/sqlalchemy/
essentially, you'll want split things few more files prevent import issues , make things little more clean. i've done below seems work. note, i've used sqlite, since not have postgres installed on box.
app.py
from flask import flask app = flask(__name__) app.config['sqlalchemy_database_uri'] = 'sqlite:////test11.db' models.py
from flask.ext.sqlalchemy import sqlalchemy app import app db = sqlalchemy(app) 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 = (password) def check_password(self, password): return password routes.py
from models import user, db db.create_all() db.session.commit() admin = user('admin', 'admin@example.com', 'admin1', 'admin1@example.com') guest = user('admi2', 'admin@ex1ample.com', 'admin', 'admin2@example.com') db.session.add(admin) db.session.add(guest) db.session.commit() i'd suggest looking on tutorials! you'll need it: should learn web vulnerabilities, best practices, , on.

Comments
Post a Comment