3

I'm new to SqlAlchemy. We were working primarily with Flask, but in a particular case I needed a manual database connection. So I launched a new db connection with something like this:

write_engine = create_engine("mysql://user:pass@localhost/db?charset=utf8")
write_session = scoped_session(sessionmaker(autocommit=False,
    autoflush=False,bind=write_engine))

nlabel = write_session.query(Label).filter(Label.id==label.id).first() # Works
#Later in code
ms = Message("Some message")
write_session.add(ms) # Works fine
write_session.commit() # Errors out

Error looks like "AttributeError: 'SessionMaker' object has no attribute '_model_changes'"

What am I doing wrong?

3

2 Answers 2

3

From the documentation I think you might be missing the initialization of the Session object.

Try:

Session = scoped_session(sessionmaker(autocommit=False, autoflush=False,bind=write_engine))
write_session = Session()

It's a shot in the dark- I'm not intimately familiar with SQLAlchemy. Best of luck!

Sign up to request clarification or add additional context in comments.

Comments

2

Your issue is that you are missing this line:

db_session._model_changes = {}

2 Comments

please add some explanation of your solution and how it resolves the problem
@Philip, because flask attaches a static method for the session. Hence, before commit it checks _model_changes. See flask_sqlclchemy's init.py. Here is the function:def session_signal_before_commit(session): d = session._model_changes if d: before_models_committed.send(session.app, changes=d.values())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.