python - Select query fails in sqlalchemy -
please having issue first sqlachemy app. trying query db posgres , return row matches . below code.
app.py
from sqlalchemy.orm import sessionmaker models.models import base,user,product,productitem,database, initialize engine = database base.metadata.bind = (engine) dbsession = sessionmaker(bind=engine) session = dbsession() .... @app.route('/login', methods = ['get','post']) def login(): form = registerform.loginform() if request.method == 'get': return render_template('login.html', form=form) return authenticate(form = form) def authenticate(form): if form.validate_on_submit(): try: user = session.query(user).filter(user.email == 'xxxx@yahoo.com') if session.query(exists().where(user.email == form.email.data)).scalar() : return user.name except :# models.doesnotexist: flash("your email or password not match !", "error") return 'error'
my modules in seperate folder modules
modules/modules.py
#declarations base = declarative_base() engine = create_engine('postgresql://postgres:0102443167@localhost:5432/postgres',echo=true) base.metadata.bind = (engine) dbsession = sessionmaker(bind=engine) session = dbsession() database = engine class user(usermixin , base): __tablename__ = 'users' id = column(integer, primary_key=true) title = column(char(3), nullable = false) fname = column(string(100), nullable = false) lname = column(string(100), nullable = false) username = column(string(100), nullable = false, unique = true) email = column (string(50), nullable =false, unique = true) password = column(string(100), nullable = false) address = column(string(250), nullable = false) state = column(string(50), nullable = false) is_admin = column(boolean ,default = false) is_logged = column(boolean, default = false) is_active = column (boolean , default = false) is_block = column(boolean, default = false) joined_on = column(arrowtype) ......
my problem app.py
authenticate method. code
user = session.query(user).filter(user.email == form.email.data)
raise error. when check form.email.data
return correct string. when tried
if session.query(exists().where(user.email == form.email.data)).scalar() : return 'ok'
works expected. works perfect , make me realize issue might query . tried
if session.query(exists().where(user.email == form.email.data)).scalar() : return user.lname
and raise error saying user not have property name should have. confuse. please how query record tables ? appreciated
you have first object matches query have add .first()
@ end of query:
user = session.query(user).filter(user.email == form.email.data).first()
see here more info.
Comments
Post a Comment