IntegrityError:尝试保存模型实例时的NOT NULL约束

时间:2018-02-16 20:20:04

标签: python django

我知道这已被问了很多,但没有一个答案可以帮助我

我有两个模型:一个存储邮件,另一个存储发件人信息:

class SenderInfo(models.Model):
    #to create unique id numbers
    sender = models.CharField(max_length=15)
    id_number = models.IntegerField(blank=True, null=True, default=None)

class Messages(models.Model):
    message = models.ForeignKey(SenderInfo, related_name='messages')
    message_body = models.TextField()

当我尝试制作一个实例并save()时,我会继续IntegrityError: NOT NULL constraint failed: appconvo_messages.message_id

完整追溯:

>>> from appconvo.models import SenderInfo, Messages
>>> s = SenderInfo(sender='charles')
>>> s.save()
>>> m = Messages(message_body='this is a test')
>>> m.save()
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Users\ELITEBOOK\.virtualenvs\codechallenge\lib\site-packages\django\db\models\base.py", line 808, in save
    force_update=force_update, update_fields=update_fields)
  File "C:\Users\ELITEBOOK\.virtualenvs\codechallenge\lib\site-packages\django\db\models\base.py", line 838, in save_bas
e
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "C:\Users\ELITEBOOK\.virtualenvs\codechallenge\lib\site-packages\django\db\models\base.py", line 924, in _save_ta
ble
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "C:\Users\ELITEBOOK\.virtualenvs\codechallenge\lib\site-packages\django\db\models\base.py", line 963, in _do_inse
rt
    using=using, raw=raw)
  File "C:\Users\ELITEBOOK\.virtualenvs\codechallenge\lib\site-packages\django\db\models\manager.py", line 85, in manage
r_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\ELITEBOOK\.virtualenvs\codechallenge\lib\site-packages\django\db\models\query.py", line 1076, in _inser
t
    return query.get_compiler(using=using).execute_sql(return_id)
  File "C:\Users\ELITEBOOK\.virtualenvs\codechallenge\lib\site-packages\django\db\models\sql\compiler.py", line 1112, in
 execute_sql
    cursor.execute(sql, params)
  File "C:\Users\ELITEBOOK\.virtualenvs\codechallenge\lib\site-packages\django\db\backends\utils.py", line 79, in execut
e
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "C:\Users\ELITEBOOK\.virtualenvs\codechallenge\lib\site-packages\django\db\backends\utils.py", line 64, in execut
e
    return self.cursor.execute(sql, params)
  File "C:\Users\ELITEBOOK\.virtualenvs\codechallenge\lib\site-packages\django\db\utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:\Users\ELITEBOOK\.virtualenvs\codechallenge\lib\site-packages\django\db\backends\utils.py", line 64, in execut
e
    return self.cursor.execute(sql, params)
  File "C:\Users\ELITEBOOK\.virtualenvs\codechallenge\lib\site-packages\django\db\backends\sqlite3\base.py", line 328, i
n execute
    return Database.Cursor.execute(self, query, params)
IntegrityError: NOT NULL constraint failed: appconvo_messages.message_id
>>>

1 个答案:

答案 0 :(得分:3)

根据您的代码,您需要在Message对象中指定SenderInfo。

>>> s = SenderInfo(sender='charles')
>>> s.save()
>>> m = Messages(message=s, message_body='this is a test')
>>> m.save()

或者相同:

m = Messages.objects.create(message=s, message_body='your message')