Invalid default value for user_id_id in django
Question: I am new to django and I am creating my models but I am having trouble when
trying to add a foreign key to another model. here's my models:

    
    
    from django.db import models
    
    class User(models.Model):
        user_id = models.CharField(max_length=10, primary_key=True)
        name = models.CharField(max_length=30)
        surname = models.CharField(max_length=30)
        role = models.CharField(max_length=10)
        address = models.CharField(max_length=50)
        email = models.EmailField(max_length=30)
        password = models.CharField(max_length=20)
        phone = models.IntegerField()
        GPA = models.FloatField(max_length=5)
        Gender = models.CharField(max_length=1)
    
        def __str__(self):
            return self.user_id
    
    
    class Login(models.Model):
        user_id = models.ForeignKey(User, on_delete=models.CASCADE,  default='00000')
        email = models.EmailField(max_length=30)
        password = models.CharField(max_length=20)
    
        def __str__(self):
            return self.email
    

When I type makemigrations I get this:

    
    
    You are trying to change the nullable field 'user_id' on login to non-nullable without a default; we can't do that (the database needs something to populate existing rows).
    Please select a fix:
     1) Provide a one-off default now (will be set on all existing rows)
     2) Ignore for now, and let me handle existing rows with NULL myself (e.g. because you added a RunPython or RunSQL operation to handle NULL values in a previous data migration)
     3) Quit, and let me add a default in models.py
    Select an option:  3
    

So I added a default value but I am getting this error when I tried to
migrate. I tried to change user_id from User to AutoField so I don't have to
add any default value but it is still giving me this error. Plus I don't know
why it says user_id_id at the end. Can anyone help me out with this?

    
    
        Running migrations:
          Rendering model states... DONE
          Applying login.0003_login_user_id...Traceback (most recent call last):
          File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\backends\utils.py", line 64, in execute
            return self.cursor.execute(sql, params)
          File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\backends\mysql\base.py", line 112, in execute
            return self.cursor.execute(query, args)
          File "C:\User\AppData\Local\Programs\Python\Python35-32\lib\site-packages\MySQLdb\cursors.py", line 226, in execute
            self.errorhandler(self, exc, value)
          File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\MySQLdb\connections.py", line 42, in defaulterrorhandler
            raise errorvalue
          File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\MySQLdb\cursors.py", line 223, in execute
            res = self._query(query)
          File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\MySQLdb\cursors.py", line 379, in _query
            rowcount = self._do_query(q)
          File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\MySQLdb\cursors.py", line 342, in _do_query
            db.query(q)
          File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\MySQLdb\connections.py", line 286, in query
            _mysql.connection.query(self, query)
        _mysql_exceptions.OperationalError: (1067, "Invalid default value for 'user_id_id'")
    
        The above exception was the direct cause of the following exception:
    
        Traceback (most recent call last):
          File "C:\Program Files (x86)\JetBrains\PyCharm 5.0.4\helpers\pycharm\django_manage.py", line 41, in <module>
            run_module(manage_file, None, '__main__', True)
          File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\runpy.py", line 182, in run_module
            return _run_module_code(code, init_globals, run_name, mod_spec)
          File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\runpy.py", line 96, in _run_module_code
            mod_name, mod_spec, pkg_name, script_name)
          File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\runpy.py", line 85, in _run_code
            exec(code, run_globals)
          File "C:/Users/Desktop/Project/TSL/mysite\manage.py", line 10, in <module>
            execute_from_command_line(sys.argv)
          File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\management\__init__.py", line 353, in execute_from_command_line
            utility.execute()
          File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\management\__init__.py", line 345, in execute
            self.fetch_command(subcommand).run_from_argv(self.argv)
          File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\management\base.py", line 348, in run_from_argv
            self.execute(*args, **cmd_options)
          File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\management\base.py", line 399, in execute
            output = self.handle(*args, **options)
          File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\management\commands\migrate.py", line 200, in handle
            executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
          File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\migrations\executor.py", line 92, in migrate
            self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)
          File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\migrations\executor.py", line 121, in _migrate_all_forwards
            state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
          File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\migrations\executor.py", line 198, in apply_migration
            state = migration.apply(state, schema_editor)
          File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\migrations\migration.py", line 123, in apply
            operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
          File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\migrations\operations\fields.py", line 62, in database_forwards
            field,
          File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\backends\mysql\schema.py", line 50, in add_field
            super(DatabaseSchemaEditor, self).add_field(model, field)
          File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\backends\base\schema.py", line 396, in add_field
            self.execute(sql, params)
          File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\backends\base\schema.py", line 110, in execute
            cursor.execute(sql, params)
          File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\backends\utils.py", line 79, in execute
            return super(CursorDebugWrapper, self).execute(sql, params)
          File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\backends\utils.py", line 64, in execute
            return self.cursor.execute(sql, params)
          File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\utils.py", line 95, in __exit__
            six.reraise(dj_exc_type, dj_exc_value, traceback)
          File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\utils\six.py", line 685, in reraise
            raise value.with_traceback(tb)
          File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\backends\utils.py", line 64, in execute
            return self.cursor.execute(sql, params)
          File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\backends\mysql\base.py", line 112, in execute
            return self.cursor.execute(query, args)
          File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\MySQLdb\cursors.py", line 226, in execute
            self.errorhandler(self, exc, value)
          File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\MySQLdb\connections.py", line 42, in defaulterrorhandler
            raise errorvalue
          File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\MySQLdb\cursors.py", line 223, in execute
            res = self._query(query)
          File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\MySQLdb\cursors.py", line 379, in _query
            rowcount = self._do_query(q)
          File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\MySQLdb\cursors.py", line 342, in _do_query
            db.query(q)
          File "C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site-packages\MySQLdb\connections.py", line 286, in query
            _mysql.connection.query(self, query)
        django.db.utils.OperationalError: (1067, "Invalid default value for 'user_id_id'")
    
        Process finished with exit code 1
    


Answer: First of all, as mentioned in the comments, do NOT name the `ForeignKey` field
`user_id`, but `user`. This will create a column called `user_id` in the db
table and your model instance's `user` attribute will return a `User` instance
while its auto-generated attribute `user_id` will return that user's id.

As for specifying a default value for a `ForeignKey`. If you do that on the
model, make sure you provide an **existing** User. If you choose to provide a
one-off default during `makemigrations` (if you select option 1), make sure to
provide the primary key of an **existing** User. Alternatively, make sure
there are no existing `Login` records in the db.

