Forums

Queries not taking effect

Hey! I'm developing a commercial website with user accounts. As of today, any new query I write (executing MySQL query through Python) is just not being executed. All the older ones are, but the new ones aren't. When I write the same query in the console it is working. It's driving me crazy. Any help will be welcome. Thanks!

Could you give examples of old and new queries?

Is it possible one or more commits are missing?

Are you using a layer between Python and MySQL, e.g. SqlAlchemy or PyMySQL or whatever?

Jim

Are you reloading your web app between attempts?

Yes, I'm reloading the app. Also, I'm not using Django's standard query API, but rather Pyhton's, i.e. MySQLdb. All earlier queries of the same type worked to perfection.

Oh! Got it! I wasn't commiting after every insert, update and delete. Thanks for pointing that out.

Well, you don't have to commit after every single operation - it all depends on if/how the operations are grouped logically, and if/how you're using database transactions. But I'm glad it helped!

Jim

+1 to what Jim said. A sensible rule of thumb is to commit on successful completion of a view function, or rollback if a view fails -- that's essentially what Django does by default. Though obviously if you have points halfway through a view where it makes logical sense (in terms of the app you're building) to commit or rollback, it's fine to do it there too.

I usually commit at the end of each view, before returning an HTTP object

That sounds perfect. Don't forget to roll back if you get an exception, though.