Stack Overflow.

I have a Django app that manages Item listings and Tags (categories). They are related with a `ManyToMany` field on the `Item` model. The following code is of the relevant code from the models

I am using PostgreSQL as my database.

```py
#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)

```

And the `Item` model

```py
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)

```

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 `<select>` menu and tried to save it.

However, I was greeted with the following error message

> 
> null value in column "tag\_id" of relation "item\_item" violates not-null constraint
> 
> 
> 

and

> 
> DETAIL: Failing row contains (18, Item Name Here, This is the description, 40.00, items/pattern.png, lXBjgo70QIrI8aF, 1, null).
> 
> 
> 

This is the full traceback

```py
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 "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)

The above exception (null value in column "tag_id" of relation "item_item" 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 "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\admin\options.py", line 616, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\decorators.py", line 130, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\views\decorators\cache.py", line 44, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\admin\sites.py", line 232, in inner
    return view(request, *args, **kwargs)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\admin\options.py", line 1657, in add_view
    return self.changeform_view(request, None, form_url, extra_context)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\decorators.py", line 43, in _wrapper
    return bound_method(*args, **kwargs)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\decorators.py", line 130, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\admin\options.py", line 1540, in changeform_view
    return self._changeform_view(request, object_id, form_url, extra_context)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\admin\options.py", line 1586, in _changeform_view
    self.save_model(request, new_object, form, not add)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\admin\options.py", line 1099, in save_model
    obj.save()
  File "C:\Users\User\camelo\cameloapp\item\models.py", line 104, in save
    super().save(*args, **kwargs)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py", line 726, in save
    self.save_base(using=using, force_insert=force_insert,
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py", line 763, in save_base
    updated = self._save_table(
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py", line 868, in _save_table
    results = self._do_insert(cls._base_manager, using, fields, returning_fields, raw)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py", line 906, in _do_insert
    return manager._insert(
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\query.py", line 1270, in _insert
    return query.get_compiler(using=using).execute_sql(returning_fields)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\sql\compiler.py", line 1416, in execute_sql
    cursor.execute(sql, params)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py", line 98, in execute
    return super().execute(sql, params)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)

Exception Type: IntegrityError at /admin/item/item/add/
Exception Value: null value in column "tag_id" of relation "item_item" violates not-null constraint
DETAIL:  Failing row contains (18, Item Name Here, This is the description, 40.00, items/pattern.png, lXBjgo70QIrI8aF, 1, null).

```

Sorry for all of the blocks of code/errors

What I have tried
-----------------

I have tried a multitude of things to try and rid this error, but none have worked.

* Deleted the migrations
* Added, removed, readded `null=True` on both models' slug fields
* Did the same thing for `blank=True`

None of these have worked. I have looked around online for a pre-existing solution, but they have only led me to adding `null=True` on my fields, which I already have.

I am very confused as to why this error is occurring.

`tag_id` stands out to me, as I assume the not-null error relates to that, and I assumed `id`s are assigned at the creation of an object.

Thank you in advance