Django,postgres:IntegrityError列中的值为null,但列应接受null
我有这个模型:Django,postgres:IntegrityError列中的值为null,但列应接受null,django,python-unittest,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", kwarg
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应该接受空值。确实如此。例如,我在shell上创建了它,它工作正常:
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>
错误跟踪是:
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).
并在表定义中,转换为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) | | |
也许您更改了代码,但这些更改没有在数据库中进行。 您可以尝试运行:
/manage.py makemigrations&/manage.py migrate
然后再试一次。
参考文档。我意识到我的pytest配置中有这个
[pytest]
addopts = --nomigrations --reuse-db
所以,迁移并没有应用于测试数据库。(以前需要段塞字段,现在不需要)
所以我解决了它,使用--createdb参数执行
pytest posts/tests/test_api_post.py --create-db
参考:对不起,我没有澄清这一点。。没有挂起的迁移。。。同样在psql表检查中,它在“notnull”列中没有说明任何内容。。我会更新这个info@Gonzalo这是对测试数据库的检查吗?您可以使用--keepdb运行测试,以便检查测试数据库meanwhile@Gonzalo您应该检查正确的数据库,测试中使用的数据库将具有不同的名称True!在测试数据库中,slug字段显示为“非空”。。这就是问题所在,就像测试数据库中没有应用迁移一样。@Gonzalo在更改模式时,不迁移地重用数据库不是一个好主意。至少现在你知道原因了:)
[pytest]
addopts = --nomigrations --reuse-db
pytest posts/tests/test_api_post.py --create-db