ProgrammingError: (1146, “Table 'test_.' doesn't exist”) when running unit test for Django
前端 未结 3 1199
后悔当初
后悔当初 2021-02-20 13:34

I\'m running a unit test using the Django framework and get this error.

Running the actual code does not have this problem, running the unit tests creates a test databas

相关标签:
3条回答
  • 2021-02-20 13:52

    On a slightly different note, you definitely do not want this:

    created_on = models.DateTimeField(…, default=datetime.datetime.utcnow())
    

    This actually calls utcnow() when your models.py file is loaded (which will happen when you manage.py runserver) and use that single point in time for the default value for every instance there-after. What you want is this:

    created_on = models.DateTimeField(…, default=datetime.datetime.utcnow)
    

    The default argument can accept a callable, which will be called each time the default value is needed for a new instance. Have a look at the documentation.

    0 讨论(0)
  • 2021-02-20 13:57

    To rectify this problem generate all your table which were declared in the settings.py file in your project folder.

    You can find the in the INSTALLED APPS Block in the settings file. For that run this command:

    manage.py syncdb or python manage.py syncdb

    If this doesn't work then set the Environment variable PATH for the python directory.

    0 讨论(0)
  • 2021-02-20 14:00

    You may get more information on what is going on by adding -v3 to your test, ie:

    $ python manage.py test -v3 sitecoming
    
    0 讨论(0)
提交回复
热议问题