Q:

IntegrityError: NOT NULL constraint when trying to save a model instance

I know this has been asked a lot but none of the answers were able to help me
I have two models: one that stores messages and one that stores sender info:
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()

And when I try to make an instance and save() it I keep getting IntegrityError: NOT NULL constraint failed: appconvo_messages.message_id
Full traceback:
>>> 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
>>>

A:

Based on your code, you need to specify the SenderInfo in your Message object.
>>> s = SenderInfo(sender='charles')
>>> s.save()
>>> m = Messages(message=s, message_body='this is a test')
>>> m.save()

Or the same:
m = Messages.objects.create(message=s, message_body='your message')

