Missing Table When Running Django Unittest with Sqlite3

前端 未结 12 1867
半阙折子戏
半阙折子戏 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-04 00:11

    Your database is probably empty, it must be setup with all the tables corresponding to your models. Normally, this would be done by running python manage.py syncdb first, to create all your database tables. The problem is that in your case, when you run syncdb, python will not see that you are running a test so it will try to setup tables in your MySQL database instead.

    To get around this, temporarily change

    if 'test' in sys.argv:
    

    to

    if True:
    

    Then run python manage.py syncdb to setup the sqlite database tables. Now that everything is setup, you can put back in if 'test'... and everything should run smoothly. However you probably want to move your database out of the /tmp directory: django needs to re-use the same database every time you run your tests, otherwise you'll have to create database tables before every test.

    Note that if you add new models, you will need to repeat this procedure to create the new tables in sqlite. If you add new fields to an existing model, you will need to manually add columns to your sqlite database using the sqlite interface and ALTER TABLE..., or do it automatically using a tool like South.

提交回复
热议问题