python - SQLAlchemy Object already attached to session -
i'm trying server app working, i'm getting error upon login:
[!] object '<user @ 0x7f12bc185a90>' attached session '2' (this '3')
it seems session i'm adding on database. snippet of code causing problem:
@app.route('/login', methods=['post']) def login(): u = user.query.filter(user.username == request.form["username"]).first() if not u or u.password != request.form["password"]: homecoming error("e1") s = session.get_by_user(u) if s not none: db_session.delete(s) db_session.commit() print db_session.execute("select * sessions").fetchall() s = session(u) db_session.add(s) db_session.commit() homecoming jsonify(s.values)
as can see, i'm printing content sessions table before trying add together anything, , empty! ([])
what else causing this?
here 'session' implementation:
class session(base): __tablename__ = "sessions" id = column(integer, primary_key=true) user_id = column(integer, foreignkey('users.id'), unique=true) user = relationship(user) key = column(string(50), unique=true) created = column(datetime) def __init__(self, user=none): self.user = user self.key = base64.encodestring(os.urandom(24)).strip() self.created = datetime.now() def __repr__(self): homecoming '<session %r>' % (self.key) @property def values(self): homecoming {"username" : self.user.username, "key" : self.key, "created" : str(self.created), } @classmethod def get_by_key(cls, key): s = cls.query.filter(cls.key == key).first() #print datetime.now() - s.created if s , datetime.now() - s.created > settings.session_lifetime: s = none homecoming s @classmethod def get_by_user(cls, user): s = cls.query.filter(cls.user == user).first() if s , datetime.now() - s.created > settings.session_lifetime: s.query.delete() db_session.commit() s = none homecoming s
object you're trying modify attached session. maybe have wrong imports, , db_session new instance.
a workaround extract current bound session , utilize it:
instead of:
db_session.add(s)
do:
current_db_sessions = db_session.object_session(s) current_db_sessions.add(s)
python session python-2.7 sqlalchemy flask-sqlalchemy
No comments:
Post a Comment