I am writing test cases for a Flask application.
I have a setUp method which drops the tables in the db before re-creating them again. It looks like this:
def setUp(self): # other stuff... myapp.db.drop_all() myapp.db.create_all() # db creation...
This works fine for the first test, but it freezes at drop_all
before the second test is run.
EDIT: The stack trace looks like this when interrupting the process
File "populate.py", line 70, in create_test_db print (myapp.db.drop_all()) File ".../flask_sqlalchemy/__init__.py", line 864, in drop_all self._execute_for_all_tables(app, bind, 'drop_all') File ".../flask_sqlalchemy/__init__.py", line 848, in _execute_for_all_tables op(bind=self.get_engine(app, bind), tables=tables) File ".../sqlalchemy/sql/schema.py", line 3335, in drop_all .... File "/Library/Python/2.7/site-packages/MySQLdb/cursors.py", line 190, in execute r = self._query(query)
Anybody has a clue how to fix this?
Oki, there might be other solutions but for now, after searching the interwebs, I found that the problem disappears if I prepend my code with a myapp.db.session.commit()
. I guess, somewhere a transaction was waiting to be committed.
def setUp(self): # other stuff... myapp.db.session.commit() #<--- solution! myapp.db.drop_all() myapp.db.create_all() # db creation...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With