title: null value in column ... violates not-null constraint
body: <p>Stack Overflow.</p>
  <p>I have a Django app that manages Item listings and Tags (categories). They are related with a <code>ManyToMany</code> field on the <code>Item</code> model. The following code is of the relevant code from the models</p>
  <p>I am using PostgreSQL as my database.</p>
  <pre class="lang-py prettyprint-override"><code>#models.py
  
  class Tag(models.Model):
      title = models.CharField(max_length=300, unique=True)
      slug = models.SlugField(max_length=300, blank=True, null=True, unique=True)
      objects = TagManager()
  
      def generate_token(self):
          self.slug = get_random_string(length=15)
          return self.slug
  
      def save(self, *args, **kwargs):
          if self.slug is None:
              self.generate_token()
          super().save(*args, **kwargs)
  </code></pre>
  <p>And the <code>Item</code> model</p>
  <pre class="lang-py prettyprint-override"><code>class Item(models.Model):
      seller = models.ForeignKey(User,related_name='seller', on_delete=models.CASCADE)
      
      #the following lines are the problem lines
      tag = models.ManyToManyField(Tag, related_name='tag', blank=True) 
      slug = models.SlugField(max_length=30, blank=True, null=True, unique=True)
  
      objects = ItemManager()
  
      def generate_token(self):
          self.slug = get_random_string(length=15)
          return self.slug
  
      def save(self, *args, **kwargs):
          if self.slug is None:
              self.generate_token()
          super().save(*args, **kwargs)
  </code></pre>
  <p>This code ran fine until I tried adding an item from the Django Admin page. I filled in all of the fields, added two tags from the <code>&lt;select&gt;</code> menu and tried to save it.</p>
  <p>However, I was greeted with the following error message</p>
  <blockquote>
  <p>null value in column &quot;tag_id&quot; of relation &quot;item_item&quot; violates not-null constraint</p>
  </blockquote>
  <p>and</p>
  <blockquote>
  <p>DETAIL:  Failing row contains (18, Item Name Here, This is the description, 40.00, items/pattern.png, lXBjgo70QIrI8aF, 1, null).</p>
  </blockquote>
  <p>This is the full traceback</p>
  <pre class="lang-py prettyprint-override"><code>Environment:
  
  
  Request Method: POST
  Request URL: http://127.0.0.1:8000/admin/item/item/add/
  
  Django Version: 3.2.6
  Python Version: 3.9.6
  Installed Applications:
  ['django.contrib.admin',
   'django.contrib.sites',
   'django.contrib.auth',
   'django.contrib.contenttypes',
   'django.contrib.sessions',
   'django.contrib.messages',
   'django.contrib.staticfiles',
   'cameloapp',
   'user',
   'item',
   'allauth',
   'allauth.account',
   'allauth.socialaccount',
   'allauth.socialaccount.providers.google',
   'allauth.socialaccount.providers.twitter']
  Installed Middleware:
  ['django.middleware.security.SecurityMiddleware',
   'django.contrib.sessions.middleware.SessionMiddleware',
   'django.middleware.common.CommonMiddleware',
   'django.middleware.csrf.CsrfViewMiddleware',
   'django.contrib.auth.middleware.AuthenticationMiddleware',
   'django.contrib.messages.middleware.MessageMiddleware',
   'django.middleware.clickjacking.XFrameOptionsMiddleware']
  
  
  
  Traceback (most recent call last):
    File &quot;C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py&quot;, line 84, in _execute
      return self.cursor.execute(sql, params)
  
  The above exception (null value in column &quot;tag_id&quot; of relation &quot;item_item&quot; violates not-null constraint
  DETAIL:  Failing row contains (18, Item Name Here, This is the description, 40.00, items/pattern.png, lXBjgo70QIrI8aF, 1, null).
  ) was the direct cause of the following exception:
    File &quot;C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py&quot;, line 47, in inner
      response = get_response(request)
    File &quot;C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py&quot;, line 181, in _get_response
      response = wrapped_callback(request, *callback_args, **callback_kwargs)
    File &quot;C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\admin\options.py&quot;, line 616, in wrapper
      return self.admin_site.admin_view(view)(*args, **kwargs)
    File &quot;C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\decorators.py&quot;, line 130, in _wrapped_view
      response = view_func(request, *args, **kwargs)
    File &quot;C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\views\decorators\cache.py&quot;, line 44, in _wrapped_view_func
      response = view_func(request, *args, **kwargs)
    File &quot;C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\admin\sites.py&quot;, line 232, in inner
      return view(request, *args, **kwargs)
    File &quot;C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\admin\options.py&quot;, line 1657, in add_view
      return self.changeform_view(request, None, form_url, extra_context)
    File &quot;C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\decorators.py&quot;, line 43, in _wrapper
      return bound_method(*args, **kwargs)
    File &quot;C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\decorators.py&quot;, line 130, in _wrapped_view
      response = view_func(request, *args, **kwargs)
    File &quot;C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\admin\options.py&quot;, line 1540, in changeform_view
      return self._changeform_view(request, object_id, form_url, extra_context)
    File &quot;C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\admin\options.py&quot;, line 1586, in _changeform_view
      self.save_model(request, new_object, form, not add)
    File &quot;C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\admin\options.py&quot;, line 1099, in save_model
      obj.save()
    File &quot;C:\Users\User\camelo\cameloapp\item\models.py&quot;, line 104, in save
      super().save(*args, **kwargs)
    File &quot;C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py&quot;, line 726, in save
      self.save_base(using=using, force_insert=force_insert,
    File &quot;C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py&quot;, line 763, in save_base
      updated = self._save_table(
    File &quot;C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py&quot;, line 868, in _save_table
      results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
    File &quot;C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py&quot;, line 906, in _do_insert
      return manager._insert(
    File &quot;C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\manager.py&quot;, line 85, in manager_method
      return getattr(self.get_queryset(), name)(*args, **kwargs)
    File &quot;C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\query.py&quot;, line 1270, in _insert
      return query.get_compiler(using=using).execute_sql(returning_fields)
    File &quot;C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\sql\compiler.py&quot;, line 1416, in execute_sql
      cursor.execute(sql, params)
    File &quot;C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py&quot;, line 98, in execute
      return super().execute(sql, params)
    File &quot;C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py&quot;, line 66, in execute
      return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
    File &quot;C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py&quot;, line 75, in _execute_with_wrappers
      return executor(sql, params, many, context)
    File &quot;C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py&quot;, line 84, in _execute
      return self.cursor.execute(sql, params)
    File &quot;C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\utils.py&quot;, line 90, in __exit__
      raise dj_exc_value.with_traceback(traceback) from exc_value
    File &quot;C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py&quot;, line 84, in _execute
      return self.cursor.execute(sql, params)
  
  Exception Type: IntegrityError at /admin/item/item/add/
  Exception Value: null value in column &quot;tag_id&quot; of relation &quot;item_item&quot; violates not-null constraint
  DETAIL:  Failing row contains (18, Item Name Here, This is the description, 40.00, items/pattern.png, lXBjgo70QIrI8aF, 1, null).
  
  </code></pre>
  <p>Sorry for all of the blocks of code/errors</p>
  <h2>What I have tried</h2>
  <p>I have tried a multitude of things to try and rid this error, but none have worked.</p>
  <ul>
  <li>Deleted the migrations</li>
  <li>Added, removed, readded <code>null=True</code> on both models' slug fields</li>
  <li>Did the same thing for <code>blank=True</code></li>
  </ul>
  <p>None of these have worked. I have looked around online for a pre-existing solution, but they have only led me to adding <code>null=True</code> on my fields, which I already have.</p>
  <p>I am very confused as to why this error is occurring.</p>
  <p><code>tag_id</code> stands out to me, as I assume the not-null error relates to that, and I assumed <code>id</code>s are assigned at the creation of an object.</p>
  <p>Thank you in advance</p>