Adding to Mike's response...

I think you're using two sessions...

you select this:

    user = Users.query.filter_by(username='foo').first()

and save this

    db.session.add(user)

when you select, i think that's using some flask-sqlalchemy syntactic sugar 
to select the session.  a 'raw' sqlalchemy approach would typically be:

    db.session.query(Users).filter_by(username='foo').first()

Your error when saving made this stick out:

  sqlalchemy.exc.InvalidRequestError: Object '<UserInfo at 0x7f3bf01b3d30>' 
is already attached to session '1' (this is '2')

If that's the case, these aren't being issued against the session you 
queried with

    db.session.flush()
    db.session.commit()

so when you do this...

    print(user.first_name)

this never hit sql.  you're just printing the object state that has not 
been flushed or committed.

All that being said, You should repost this on the Flask community 
list/slack channel. The above code is *generally correct* and your issue is 
most likely in the integration layer of flask-sqlalchemy -- and not your 
usage of sqlalchemy. They can probably solve it faster there, as most 
people on this group don't really know flask/flask-sqlalchemy (but everyone 
here would love to know your solution). 





-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to