考虑以下片段:
if fire_prim is not None:
print('Fire Primary is definitely not Null, here is proof: {}'.format(fire_prim))
fire_primary_obj = LifeSafety.objects.create(
name='Fire Primary',
store=store_obj,
phone_line=fire_prim,
phone_line_provider=an_provider)
我们得到以下输出:
Fire Primary is definitely not Null, here is proof: +16077244546
Traceback (most recent call last):
File "C:\Python\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "C:\Python\lib\site-packages\django\db\backends\sqlite3\base.py", line 298, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.IntegrityError: NOT NULL constraint failed: stores_lifesafety.phone_line
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Users\admin-dksc104694\Desktop\Dev\it-database\scripts\data.py", line 246, in update_lifesafety_devices
fire_primary_obj = LifeSafety.objects.create(name='Fire Primary', store=store_obj, phone_line=fire_prim, phone_line_provider=an_provider)
File "C:\Python\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Python\lib\site-packages\django\db\models\query.py", line 413, in create
obj.save(force_insert=True, using=self.db)
File "C:\Python\lib\site-packages\django\db\models\base.py", line 718, in save
force_update=force_update, update_fields=update_fields)
File "C:\Python\lib\site-packages\django\db\models\base.py", line 748, in save_base
updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
File "C:\Python\lib\site-packages\django\db\models\base.py", line 831, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
File "C:\Python\lib\site-packages\django\db\models\base.py", line 869, in _do_insert
using=using, raw=raw)
File "C:\Python\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Python\lib\site-packages\django\db\models\query.py", line 1136, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "C:\Python\lib\site-packages\django\db\models\sql\compiler.py", line 1289, in execute_sql
cursor.execute(sql, params)
File "C:\Python\lib\site-packages\django\db\backends\utils.py", line 100, in execute
return super().execute(sql, params)
File "C:\Python\lib\site-packages\django\db\backends\utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:\Python\lib\site-packages\django\db\backends\utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:\Python\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "C:\Python\lib\site-packages\django\db\utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:\Python\lib\site-packages\django\db\backends\utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "C:\Python\lib\site-packages\django\db\backends\sqlite3\base.py", line 298, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: NOT NULL constraint failed: stores_lifesafety.phone_line
我们可以100%地确定在创建对象之前的那一行fire_prim
的值为+16077244546
,应该用作phone_line
,但是到了创建它以某种方式删除。
这是我的模型供参考:
class LifeSafety(models.Model):
name = models.CharField(max_length=40, default='unspecified')
store = models.ForeignKey(Store, null=True, on_delete=models.SET_NULL)
phone_line = PhoneNumberField()
phone_line_provider = models.ForeignKey(Provider, null=True, on_delete=models.SET_NULL)
last_verification = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
前提条件:
phone_line
为空。fire_prim
的打印和创建尝试之间没有任何内容。问题后:
phone_line
更改为 CharField ,而不是 PhoneNumberField ,并且电话号码可以正常通过。我也在他们的Github Repo上发布了一个问题。phone_line
。再次确认可以从shell毫无问题地完成此操作。
输入:LifeSafety.objects.create(name ='Fire Primary',store = store,phone_line ='+ 16077244546')
输出:生命安全:消防一级(指示QuerySet)
有趣的是,当我确实在创建时明确使用了该字符串时,该对象确实使用填充该字符串的phone_line
进行了创建,但是仍然出现错误。
输入:
print('Fire Primary is definitely not Null, here is proof: {}'.format(fire_prim))
fire_primary_obj = LifeSafety.objects.create(
name = 'Fire Primary',
store = store_obj,
phone_line = '+16077244546',
phone_line_provider = an_provider
)
输出是与上面相同的堆栈跟踪(仅差异检查器中的名称有所不同)。
我可以进入admin并看到它已创建,但是我仍然遇到NOT NULL constraint
异常。