migrate with MySQL error: django.db.utils.IntegrityError: (1364, "Field 'name' doesn't have a default value")

I am new to Django and currently in the process of going through the tutorial. I am on Writing your first Django app, part 2. For database setup, I chose to use MySQL as the backend. Now, I am trying to use the migrate function on manage.py and keep encountering errors, mostly for denied user permissions, which I am granting as they are identified. However, now I am encountering a different kind of error:

django.db.utils.IntegrityError: (1364, “Field ‘name’ doesn’t have a default value”)

Even if I knew how to figure out the table to which this is referring, I am reluctant to mess with the properties and/or content of the tables, since the information being migrated for these tables seems necessary to proper functionality of the app. As a result, I am uncertain regarding how to proceed.

There would be more information in other parts of the complete traceback. It would be helpful if you posted that complete traceback, along the the models you are using.

Note: When posting code or tracebacks, enclose each between lines of three backtick - ` characters. That means you’ll have a line of ```, then the code (or traceback), then another line of ```.

py manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  No migrations to apply.
Traceback (most recent call last):
  File "C:\Users\Video\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\Video\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\mysql\base.py", line 75, in execute
    return self.cursor.execute(query, args)
  File "C:\Users\Video\AppData\Local\Programs\Python\Python310\lib\site-packages\MySQLdb\cursors.py", line 206, in execute
    res = self._query(query)
  File "C:\Users\Video\AppData\Local\Programs\Python\Python310\lib\site-packages\MySQLdb\cursors.py", line 319, in _query
    db.query(q)
  File "C:\Users\Video\AppData\Local\Programs\Python\Python310\lib\site-packages\MySQLdb\connections.py", line 254, in query
    _mysql.connection.query(self, query)
MySQLdb._exceptions.IntegrityError: (1364, "Field 'name' doesn't have a default value")

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\Video\dev\django_practice\mysite\manage.py", line 22, in <module>
    main()
  File "C:\Users\Video\dev\django_practice\mysite\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\Video\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "C:\Users\Video\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\__init__.py", line 440, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\Video\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\base.py", line 414, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\Video\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\base.py", line 460, in execute
    output = self.handle(*args, **options)
  File "C:\Users\Video\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\base.py", line 98, in wrapped
    res = handle_func(*args, **kwargs)
  File "C:\Users\Video\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\commands\migrate.py", line 317, in handle
    emit_post_migrate_signal(
  File "C:\Users\Video\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\management\sql.py", line 52, in emit_post_migrate_signal
    models.signals.post_migrate.send(
  File "C:\Users\Video\AppData\Local\Programs\Python\Python310\lib\site-packages\django\dispatch\dispatcher.py", line 176, in send
    return [
  File "C:\Users\Video\AppData\Local\Programs\Python\Python310\lib\site-packages\django\dispatch\dispatcher.py", line 177, in <listcomp>
    (receiver, receiver(signal=self, sender=sender, **named))
  File "C:\Users\Video\AppData\Local\Programs\Python\Python310\lib\site-packages\django\contrib\auth\management\__init__.py", line 51, in create_permissions
    create_contenttypes(
  File "C:\Users\Video\AppData\Local\Programs\Python\Python310\lib\site-packages\django\contrib\contenttypes\management\__init__.py", line 142, in create_contenttypes
    ContentType.objects.using(using).bulk_create(cts)
  File "C:\Users\Video\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\query.py", line 579, in bulk_create
    returned_columns = self._batched_insert(
  File "C:\Users\Video\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\query.py", line 1467, in _batched_insert
    self._insert(
  File "C:\Users\Video\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\query.py", line 1434, in _insert
    return query.get_compiler(using=using).execute_sql(returning_fields)
  File "C:\Users\Video\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\models\sql\compiler.py", line 1621, in execute_sql
    cursor.execute(sql, params)
  File "C:\Users\Video\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line 103, in execute
    return super().execute(sql, params)
  File "C:\Users\Video\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line 67, in execute
    return self._execute_with_wrappers(
  File "C:\Users\Video\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line 80, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "C:\Users\Video\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    with self.db.wrap_database_errors:
  File "C:\Users\Video\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\Video\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\Video\AppData\Local\Programs\Python\Python310\lib\site-packages\django\db\backends\mysql\base.py", line 75, in execute
    return self.cursor.execute(query, args)
  File "C:\Users\Video\AppData\Local\Programs\Python\Python310\lib\site-packages\MySQLdb\cursors.py", line 206, in execute
    res = self._query(query)
  File "C:\Users\Video\AppData\Local\Programs\Python\Python310\lib\site-packages\MySQLdb\cursors.py", line 319, in _query
    db.query(q)
  File "C:\Users\Video\AppData\Local\Programs\Python\Python310\lib\site-packages\MySQLdb\connections.py", line 254, in query
    _mysql.connection.query(self, query)
django.db.utils.IntegrityError: (1364, "Field 'name' doesn't have a default value")

So you have the text at the beginning that says:

This would lead me to believe that this has nothing specific to do with any particular migration.

I think we’d need to see the models.

Note: If you’re building your own app, I don’t see the app showing up at:

So you might also want to check your INSTALLED_APPS setting.

I was just following the instructions from the tutorial, I am about here if it helps. It never mentioned having to add that. Am I supposed to be in the mysite/mysite folder or something?

If you’re not at this point yet : Activating Models then you do need to post your models here.

Ok, so you’re at a point prior to creating the models.

You’ll need to post your settings file.

Also, can you confirm that you’ve created your MySQL database and granted the appropriate privileges?

I wasn’t sure how to attach the settings file so I just copied and pasted the code.

"""
Django settings for mysite project.

Generated by 'django-admin startproject' using Django 4.0.4.

For more information on this file, see
https://docs.djangoproject.com/en/4.0/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.0/ref/settings/
"""

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-!r@#u4e(sy)l2bar@s!tgu#8fv!g_wq5d_yt@@k8lj8$b7tz48'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

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',
]

ROOT_URLCONF = 'mysite.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'mysite.wsgi.application'


# Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mydatabase',
        'USER': 'mydatabaseuser',
        'PASSWORD': 'mypassword',
        'HOST': '127.0.0.1',
        'PORT': '3306',
    }
}


# Password validation
# https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/4.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.0/howto/static-files/

STATIC_URL = 'static/'

# Default primary key field type
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

Also, I have created the database and provided the user has the create privilege. However, I am granting other privileges as the command prompt informs me of any denied privileges that prevent execution. Example:

django.db.utils.OperationalError: (1142, "SELECT command denied to user 'mydatabaseuser'@'localhost' for table 'django_migrations'")

I’m not sure why it said no migrations to apply. Perhaps it is because I ran the command more than once.

Without having direct access to your database, it would be impossible for us to diagnose what’s going wrong.

To get you past this current issue, my immediate recommendation would be for you to follow the tutorial exactly, and that means using the sqlite database.

That would at least help identify whether the problem is with your MySQL configuration or a mistake has been made in following the tutorial.

Also, there are times when it’s most helpful just to drop / recreate the database when an error occurs - especially early on in this process.

Is there a way to “reset” the migrations? Also, I saw something about a makemigrations command. Is that relevant here? It didn’t mention it in the tutorial yet but I saw it somewhere in the migrations documentation and was wondering if perhaps that was required in the case of MySQL.

In theory, yes. You can reverse applied migrations. However, if your database is fouled to the point where migrate doesn’t work, it’s not likely that you’re going to be able to do that.

That’s not necessary until you are at the point where you’re creating your own models. Before then, it’s not necessary.