Python django中用户\u id\u id的默认值无效

Python django中用户\u id\u id的默认值无效,python,mysql,django,foreign-key-relationship,Python,Mysql,Django,Foreign Key Relationship,我是django的新手,我正在创建我的模型,但在尝试向另一个模型添加外键时遇到了问题。以下是我的模型: 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_

我是django的新手,我正在创建我的模型,但在尝试向另一个模型添加外键时遇到了问题。以下是我的模型:

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
当我键入makemigrations时,我得到以下信息:

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
因此,我添加了一个默认值,但在尝试迁移时出现了此错误。我试图将user_id从user更改为AutoField,这样我就不必添加任何默认值,但它仍然给我这个错误。另外,我也不知道为什么它最后会说user_id_id。有人能帮我解决这个问题吗

    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
运行迁移:
正在渲染模型状态。。。完成
正在应用login.0003\u login\u user\u id…回溯(最近一次呼叫最后一次):
文件“C:\Users\AppData\Local\Programs\Python35-32\lib\site packages\django\db\backends\utils.py”,第64行,在execute中
返回self.cursor.execute(sql,params)
文件“C:\Users\AppData\Local\Programs\Python35-32\lib\site packages\django\db\backends\mysql\base.py”,第112行,在execute中
返回self.cursor.execute(查询,参数)
文件“C:\User\AppData\Local\Programs\Python\Python35-32\lib\site packages\MySQLdb\cursors.py”,第226行,在execute中
errorhandler(self、exc、value)
defaulterrorhandler中的文件“C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site packages\MySQLdb\connections.py”,第42行
提高错误值
文件“C:\Users\AppData\Local\Programs\Python35-32\lib\site packages\MySQLdb\cursors.py”,第223行,在execute中
res=自我查询(查询)
文件“C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site packages\MySQLdb\cursors.py”,第379行,在查询中
rowcount=self.\u do\u查询(q)
文件“C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site packages\MySQLdb\cursors.py”,第342行,在查询中
数据库查询(q)
文件“C:\Users\AppData\Local\Programs\Python35-32\lib\site packages\MySQLdb\connections.py”,第286行,在查询中
_mysql.connection.query(self,query)
_mysql_exceptions.OperationalError:(1067,“用户id的默认值无效”)
上述异常是以下异常的直接原因:
回溯(最近一次呼叫最后一次):
文件“C:\Program Files(x86)\JetBrains\PyCharm 5.0.4\helpers\PyCharm\django_manage.py”,第41行,在
运行\u模块(管理\u文件,无,'.\u主\u',真)
文件“C:\Users\AppData\Local\Programs\Python35-32\lib\runpy.py”,第182行,在run\u模块中
返回\u运行\u模块\u代码(代码、初始化全局、运行\u名称、模块规格)
文件“C:\Users\AppData\Local\Programs\Python\Python35-32\lib\runpy.py”,第96行,在运行模块代码中
模块名称、模块规格、组件名称、脚本名称)
文件“C:\Users\AppData\Local\Programs\Python\Python35-32\lib\runpy.py”,第85行,在运行代码中
exec(代码、运行\全局)
文件“C:/Users/Desktop/Project/TSL/mysite\manage.py”,第10行,在
从命令行(sys.argv)执行命令
文件“C:\Users\AppData\Local\Programs\Python\Python 35-32\lib\site packages\django\core\management\\uuuuuu init\uuuuuu.py”,第353行,从命令行执行
utility.execute()
文件“C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site packages\django\core\management\\uuuu init\uuuu.py”,执行中第345行
self.fetch_命令(子命令)。从_argv(self.argv)运行_
文件“C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site packages\django\core\management\base.py”,第348行,从\u argv运行
self.execute(*args,**cmd_选项)
文件“C:\Users\AppData\Local\Programs\Python35-32\lib\site packages\django\core\management\base.py”,第399行,在execute中
输出=self.handle(*args,**选项)
文件“C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site packages\django\core\management\commands\migrate.py”,第200行,位于句柄中
executor.migrate(目标、计划、假=假、假首字母=假首字母)
文件“C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site packages\django\db\migrations\executor.py”,第92行,在migrate中
self.\u迁移\u所有\u转发(计划、完整计划、假=假、假首字母=假首字母)
文件“C:\Users\AppData\Local\Programs\Python\Python 35-32\lib\site packages\django\db\migrations\executor.py”,第121行,全部向前迁移
state=self.apply\u迁移(state,migration,false=false,false\u initial=false\u initial)
文件“C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site packages\django\db\migrations\executor.py”,第198行,在apply\u migration中
state=migration.apply(状态,模式编辑器)
文件“C:\Users\AppData\Local\Programs\Python35-32\lib\site packages\django\db\migrations\migration.py”,第123行,在apply中
操作。数据库转发(self.app\u标签、模式编辑器、旧状态、项目状态)
数据库中第62行的文件“C:\Users\AppData\Local\Programs\Python\Python35-32\lib\site packages\django\db\migrations\operations\fields.py”
领域
文件“C:\Users\AppData\Local\Programs\Python35-32\lib\site packages\django\db\backends\mysql\schema.py”,第50行,在add\u字段中
super(DatabaseSchemaEditor,self).add_字段(model,field)
文件“C:\Users\AppData\Local\Programs\Python35-32\lib\site packages\django\db\backends\base\schema.py”,第396行,在添加字段中
self.execute(sql,params)
文件“C:\Users\AppData\Local\Programs\Python35-32\lib\site packages\django\db\backends\base\schema.py”,第110行,在execute中
cursor.execute(sql,params)
文件“C:\Users\AppData\Local\Programs\Python35-32\lib\site packages\django\db\backends\utils.py”,第79行,在execute中
返回super(CursorDebugWrapper,self).execute(sql,params)
文件“C:\Users\AppData\Local\Programs\Python35-32\lib\site packages\django\db\backends\utils.py”,第64行,在execute中
返回self.cursor.execute(sql,params)
文件“C:\Users\AppData\Local\Programs\Python\Python35-32\lib\
from django.db import models

# Renamed from User to MyUser,
# User is a built-in model that comes with django
# and is used when logging people into the system
# https://docs.djangoproject.com/en/1.9/topics/auth/default/

class MyUser(models.Model):
    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() # email field doesn't need max_length
    password = models.CharField(max_length=20)
    phone = models.CharField(max_length=30) # this should be a CharField
    GPA = models.FloatField()
    gender = models.CharField(max_length=1,
                              choices=(('M', 'Male',),
                                       ('F', 'Female',),
                                       ('X', 'Prefer not to disclose',)))