I\'m trying to run a unittest with Django 1.3. Normally, I use MySQL as my database backend, but since this is painfully slow to spinup for a single unittest, I\'m using Sql
Having tried all of the above I eventually discovered another reason this can happen:-
If any of your models are not created by one of your migrations.
I did some debugging and it seems that Django testing sets up the database by applying all your migrations in order, starting with 001_initial.py, before trying to SELECT from the tables based on your models.py
In my case a table had somehow not been added to the migrations, but added manually, so the full migration set couldn't be properly applied. When I manually fixed the 001_initial.py migration to create this table the OperationalError went away.
Just to add another case to this:
If you are trying to upgrade to 1.8 from 1.6 (or from a non-migration setup to a migration setup), you might hit this error if you haven't run created migrations.
I had the same problem and had to create migrations so the test runner could use them, which was not intuitive because pre-migrations, the tests would just make a new DB based on doing syncdb, which always worked.
I had a similar problem and I was caused by a previous branch code, so I fixed it removing the pyc files in my projects:
find -regex .*pyc | xargs sudo rm
For anyone that will get here, searching for why Django keeps creating a database regardless of the --keepdb
option. Why it says Using existing test database for alias 'default'
, but then runs a bunch of CREATE TABLE
statements.
If you don't set a DATABASES > default > TEST > NAME setting, Django will try to use in memory database, and it wont be kept, so set this and override a defaults.
You can make it like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(ROOT_DIR, 'data', 'db.dev.sqlite3'),
'TEST': {
'NAME': os.path.join(ROOT_DIR, 'data', 'db.test.sqlite3'),
}
}
}
I had this problem, too. Turned out that I had to add a TEST_NAME property in the settings.py file to identify the test database properly. It solved the problem for me:
if 'test' in sys.argv:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(os.path.dirname(__file__), 'test.db'),
'TEST_NAME': os.path.join(os.path.dirname(__file__), 'test.db'),
}
}
For those who tried all possible ways but still stuck in this:
Since our test code is still running in other machine but not mine, I tried to:
settings
to use sqlite3
instead of psql
(isolate the affect of database, and database settings)env
variables, and .env
file (since I used foreman
)None of those helped. Until I: