ProgrammingError: (1146, “Table 'test_.' doesn't exist”) when running unit test for Django
前端 未结 3 1208
后悔当初
后悔当初 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.

提交回复
热议问题