absent1706 / sqlalchemy-mixins

Active Record, Django-like queries, nested eager load and beauty __repr__ for SQLAlchemy

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[ActiveRecordMixin] model not persisted unless I call db.session.commit()

jonathanronen opened this issue · comments

commented

When I call model.create(...), it isn't persisted in the database unless I call db.session.commit() after. What am I doing wrong?

commented

Nevermind, there's the session autosave thing and this makes sense. My bad.

@jonathanronen I'm facing a similar issue. How did you solve it?

commented

@jonathanronen I'm facing a similar issue. How did you solve it?

I decorated my api methods (POST, PUT) with this:

def with_commit(f):
    """Decorator for API methods ensures DB commit after call.
    """

    @wraps(f)
    def decorated_function(*args, **kwargs):
        res = f(*args, **kwargs)
        db.session.commit()
        return res

    return decorated_function