Django,postgres:IntegrityError null列中的值,但列应接受null

时间:2019-03-19 09:57:39

标签: django python-unittest

我有这个模型:

Class Job(models.Model):
    // Some fields
    slug = models.SlugField(verbose_name=_("slug"), max_length=151, null=True, blank=True)

    def get_absolute_url(self):                                      
        return reverse("jobs", kwargs={"slug": self.slug})

Slug应该接受Null值。确实如此。例如,我在外壳上创建了它,它可以正常工作:

In [1]: j = Job.objects.create(title="my faked title",date_start=aware_start, date_end=aware_end, amount_to_pay=10, email='fake@gmail.com')

In [2]: j
Out[2]: <Job: my faked title>

但是,如果我要测试它,则在一个测试用例上,它会失败。

测试用例是:

class JobCreationTest(TestCase):
  def test_simple_creation(self):                                              
    import datetime                                                          
    import pytz                                                              
    aware_start = datetime.datetime(2011, 8, 15, 8, 15, 12, 0, pytz.UTC)     
    aware_end = datetime.datetime(2012, 8, 15, 8, 15, 12, 0, pytz.UTC)       
    Job.objects.create(title="my faked title",date_start=aware_start, date_end=aware_end, amount_to_pay=10, email='fake@gmail.com')

错误跟踪为:

self = <django.db.backends.utils.CursorWrapper object at 0x7fc2bc72af28>
sql = 'INSERT INTO "posts_job" ("id", "created_at", "created_by_id", "updated_at", "updated_by_id", "deleted", "title", "ema...slug") VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)'
params = (UUID('e814e054-e273-4316-b670-f39584f0b3ef'), datetime.datetime(2019, 3, 19, 9, 39, 31, 31514, tzinfo=<UTC>), None, datetime.datetime(2019, 3, 19, 9, 39, 31, 31566, tzinfo=<UTC>), None, False, ...)
ignored_wrapper_args = (False, {'connection': <django.contrib.gis.db.backends.postgis.base.DatabaseWrapper object at 0x7fc2d0486f28>, 'cursor': <django.db.backends.utils.CursorWrapper object at 0x7fc2bc72af28>})

    def _execute(self, sql, params, *ignored_wrapper_args):
        self.db.validate_no_broken_transaction()
        with self.db.wrap_database_errors:
            if params is None:
                return self.cursor.execute(sql)
            else:
>               return self.cursor.execute(sql, params)
E               django.db.utils.IntegrityError: null value in column "slug" violates not-null constraint
E               DETAIL:  Failing row contains (e814e054-e273-4316-b670-f39584f0b3ef, 2019-03-19 09:39:31.031514+00, null, 2019-03-19 09:39:31.031566+00, null, f, my faked title, fake@gmail.com, , null, null, null, null, null, null, null, null, 2011-08-15 08:15:12+00, 2012-08-15 08:15:12+00, null, null, null, , , 10, null).

更新:

$ ./manage.py makemigrations posts
No changes detected in app 'posts'
$ ./manage.py migrate posts
Operations to perform:
  Apply all migrations: posts
Running migrations:
  No migrations to apply.

然后在表定义中,输入psql

> \d+ posts_job
                        Table "public.posts_job"
        Column        |           Type           | Collation | Nullable | 
----------------------+--------------------------+-----------+----------+-
 id                   | uuid                     |           | not null | 
 title                | character varying(128)   |           | not null | 
 email                | character varying(254)   |           | not null | 
 slug                 | character varying(151)   |           |          | 

2 个答案:

答案 0 :(得分:1)

也许您更改了代码,但这些更改未在数据库中进行。 您可以尝试运行:

./manage.py makemigrations && ./manage.py migrate

,然后重试。 请参考Django migrations文档。

答案 1 :(得分:0)

我已经意识到我在pytest配置中拥有这个。

[pytest]
addopts = --nomigrations --reuse-db

因此,迁移未应用于测试数据库。 (以前是子字段,现在则不是)

所以我解决了它,使用--create-db参数执行

pytest posts/tests/test_api_post.py --create-db

REF:pytest --create-db parameter

相关问题