Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

drop_all() freezes in Flask with SQLAlchemy

Tags:

python

flask

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?

like image 766
mircealungu Avatar asked Jun 18 '14 15:06

mircealungu


Video Answer


1 Answers

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... 
like image 122
mircealungu Avatar answered Sep 28 '22 15:09

mircealungu