Missing Table When Running Django Unittest with Sqlite3

前端 未结 12 1773
半阙折子戏
半阙折子戏 2021-01-03 23:42

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

相关标签:
12条回答
  • 2021-01-03 23:51

    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.

    0 讨论(0)
  • 2021-01-03 23:54

    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.

    0 讨论(0)
  • 2021-01-03 23:54

    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
    
    0 讨论(0)
  • 2021-01-04 00:01

    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'), } } }

    0 讨论(0)
  • 2021-01-04 00:02

    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'),
           }
        }
    
    0 讨论(0)
  • 2021-01-04 00:05

    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:

    • Create a new virtual-env. (so isolate the affect of apps)
    • Clone a new repository. (isolate the affect of ignored files)
    • Set in settings to use sqlite3 instead of psql (isolate the affect of database, and database settings)
    • Check env variables, and .env file (since I used foreman)

    None of those helped. Until I:

    1. Create a new account on my machine. (So start clean)
    2. Clone the repository.
    3. Run tests -> Success
    0 讨论(0)
提交回复
热议问题