Reputation: 17
I am trying to learn python and I've picked Pyramid framework. However, I didn't want to use the ORM provided by the sqlalchemy but rather write pure sql (I am still using sqlalchemy though). I am as well using khufu_sqlalchemy to set it up.
However, I am unable to do inserts into database. Transaction is being started but it is never commited..
My code:
db = dbsession(request) db.execute('insert into users (email, password) values (:email, :password)' , {'email':email, 'password':password})
Is there a way to autocommit? The documentation is really sparse..
Upvotes: 0
Views: 302
Reputation: 23331
khufu follows the pyramid practice of using a transaction manager to handle your database connections. This means that there is code (ZopeTransactionExtension
in your sessionmaker initialization) that hooks the database session and tracks changes. When changes are detected they will be automatically committed at the end of the request if no errors occur. If you are bypassing the ORM and calling execute yourself, you can either 1) turn off the transaction manager and call commit yourself or 2) mark the session as dirty after doing execute
so that the connection will think stuff changed and call commit automatically for you. The second way is preferred because if you call commit prematurely yourself the rest of the code may fail and yet data is still persisted in your database. To do the second way, use the mark_changed
function from the extension.
from zope.sqlalchemy import mark_changed
session = DBSession()
mark_changed(session)
As a side note, if you do want to force the transaction to commit before the end of a request, you can do that via
import transaction
transaction.commit()
But again, this will happen automatically at the end of a request if no errors occur.
Upvotes: 1
Reputation: 403
http://docs.sqlalchemy.org/en/latest/core/connections.html
db.commit()
is what you are looking for. Check further documentation for autocommit.
Upvotes: 0