Python django 2 mysql数据库后端不接受0作为自动字段错误的值
我不知道为什么:makemigrations命令工作正常,但当我从浏览器插入内容时,终端会输出错误消息 文件 “D:\software\WinPython-64bit-3.5.4.0Qt5\python-3.5.4.amd64\lib\site packages\django\db\backends\mysql\operations.py”,第172行,在validate\u autopk\u值中 raise VALUERROR('数据库后端不接受0作为'VALUERROR:数据库后端不接受0作为的值 奥托菲尔德 在my note/models.py中Python django 2 mysql数据库后端不接受0作为自动字段错误的值,python,mysql,django,Python,Mysql,Django,我不知道为什么:makemigrations命令工作正常,但当我从浏览器插入内容时,终端会输出错误消息 文件 “D:\software\WinPython-64bit-3.5.4.0Qt5\python-3.5.4.amd64\lib\site packages\django\db\backends\mysql\operations.py”,第172行,在validate\u autopk\u值中 raise VALUERROR('数据库后端不接受0作为'VALUERROR:数据库后端不接受0作
class notecategory(models.Model):
categoryname = models.CharField(max_length=150,null=False,default="")
isPublic = models.BooleanField()
owner = models.ForeignKey(User,on_delete=models.CASCADE,null=True,related_name='%(class)s_requests_created')
class note2(models.Model):
category = models.ForeignKey(notecategory,on_delete=models.CASCADE,null=True)
content = models.CharField(max_length =settings.CONSTANT_DICT['noteContentLen'] ,null=True)
book = models.ForeignKey(book,on_delete=models.CASCADE,null=True)
chapter = models.ForeignKey(chapter,on_delete=models.CASCADE,null=True)
sentence = models.ForeignKey(sentence,on_delete=models.CASCADE,null=True)
anchorAt = models.IntegerField(null=True)
highlightLen = models.IntegerField(null=True)
language = models.ForeignKey(language,on_delete=models.CASCADE, null=True)
owner = models.ForeignKey(User, on_delete=models.CASCADE, null=True,related_name='%(class)s_requests_created')
在my note/views.py中
toSave = note2(book_id=int(bookId),chapter=chapterInstance, sentence_id = int(sentenceId),
category_id=int(categoryId), content=userInputNoteContent,anchorAt = int(anchorAt),highlightLen=int(highlightLen),
language=languageInstance,
owner=userInstance)
toSave.save()
在注释/migrations/001_initial.py中
class Migration(migrations.Migration):
initial = True
dependencies = [
('sentence', '0008_auto_20190118_1608'),
('language', '0001_initial'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='note2',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('content', models.CharField(max_length=150, null=True)),
('anchorAt', models.IntegerField(null=True)),
('highlightLen', models.IntegerField(null=True)),
('book', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='sentence.book')),
],
),
migrations.CreateModel(
name='notecategory',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('categoryname', models.CharField(default='', max_length=150)),
('isPublic', models.BooleanField()),
('owner', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='notecategory_requests_created', to=settings.AUTH_USER_MODEL)),
],
),
migrations.AddField(
model_name='note2',
name='category',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='note.notecategory'),
),
migrations.AddField(
model_name='note2',
name='chapter',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='sentence.chapter'),
),
migrations.AddField(
model_name='note2',
name='language',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='language.language'),
),
migrations.AddField(
model_name='note2',
name='owner',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='note2_requests_created', to=settings.AUTH_USER_MODEL),
),
migrations.AddField(
model_name='note2',
name='sentence',
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='sentence.sentence'),
),
]
完整跟踪我粘贴了另一个类似型号/应用程序的错误,因为我没有复制原始消息
[18/Jan/2019 19:46:47]“GET/readbook/?HTTP/1.1”200 5809
内部服务器错误:/savenote/
回溯(最近一次呼叫最后一次):
文件“D:\software\WinPython-64bit-3.5.4.0Qt5\python-3.5.4.amd64\lib\site packages\django\core\handlers\exception.py”,
第34行,在内部
响应=获取响应(请求)
文件“D:\software\WinPython-64bit-3.5.4.0Qt5\python-3.5.4.amd64\lib\site packages\django\core\handlers\base.py”,
第126行,在“获取”响应中
response=self.process\u异常\u由\u中间件(e,请求)
文件“D:\software\WinPython-64bit-3.5.4.0Qt5\python-3.5.4.amd64\lib\site packages\django\core\handlers\base.py”,
第124行,在_get_响应中
响应=包装的回调(请求,*回调参数,**回调参数)
savenote中第143行的文件“D:\pythonworkspace\notetaking\read\views.py”
toSave.save()
文件“D:\software\WinPython-64bit-3.5.4.0Qt5\python-3.5.4.amd64\lib\site packages\django\db\models\base.py”,第717行,保存
强制更新=强制更新,更新字段=更新字段)
文件“D:\software\WinPython-64bit-3.5.4.0Qt5\python-3.5.4.amd64\lib\site packages\django\db\models\base.py”,第747行,位于save\u base中
更新=自我。保存表格(原始、cls、强制插入、强制更新、使用、更新字段)
文件“D:\software\WinPython-64bit-3.5.4.0Qt5\python-3.5.4.amd64\lib\site packages\django\db\models\base.py”,第830行,在保存表中
结果=self.\u do\u insert(cls.\u base\u manager,using,fields,update\u pk,raw)
文件“D:\software\WinPython-64bit-3.5.4.0Qt5\python-3.5.4.amd64\lib\site packages\django\db\models\base.py”,第868行,插入
使用=使用,原始=原始)
文件“D:\software\WinPython-64bit-3.5.4.0Qt5\python-3.5.4.amd64\lib\site packages\django\db\models\manager.py”,
第82行,在manager_方法中
返回getattr(self.get_queryset(),name)(*args,**kwargs)
文件“D:\software\WinPython-64bit-3.5.4.0Qt5\python-3.5.4.amd64\lib\site packages\django\db\models\query.py”,
第1133行,插入
return query.get\u编译器(using=using).execute\u sql(return\u id)
文件“D:\software\WinPython-64bit-3.5.4.0Qt5\python-3.5.4.amd64\lib\site packages\django\db\models\sql\compiler.py”,
执行sql中的第1284行
对于sql,self.as_sql()中的参数为:
文件“D:\software\WinPython-64bit-3.5.4.0Qt5\python-3.5.4.amd64\lib\site packages\django\db\models\sql\compiler.py”,
第1237行,在as_sql中
对于self.query.objs中的obj
文件“D:\software\WinPython-64bit-3.5.4.0Qt5\python-3.5.4.amd64\lib\site packages\django\db\models\sql\compiler.py”,
第1237行,在
对于self.query.objs中的obj
文件“D:\software\WinPython-64bit-3.5.4.0Qt5\python-3.5.4.amd64\lib\site packages\django\db\models\sql\compiler.py”,
第1236行,在
[self.prepare_值(字段,self.pre_保存值(字段,对象))用于字段中的字段]
文件“D:\software\WinPython-64bit-3.5.4.0Qt5\python-3.5.4.amd64\lib\site packages\django\db\models\sql\compiler.py”,
第1178行,准备_值
value=field.get\u db\u prep\u save(value,connection=self.connection)
文件“D:\software\WinPython-64bit-3.5.4.0Qt5\python-3.5.4.amd64\lib\site packages\django\db\models\fields\related.py”,
第937行,在get_db_prep_save中
返回self.target\u字段。get\u db\u prep\u save(值,连接=连接)
文件“D:\software\WinPython-64bit-3.5.4.0Qt5\python-3.5.4.amd64\lib\site packages\django\db\models\fields\uuuu init\uuuu.py”,
第790行,在get_db_prep_save中
返回self.get_db_prep_值(值,connection=connection,prepared=False)
文件“D:\software\WinPython-64bit-3.5.4.0Qt5\python-3.5.4.amd64\lib\site packages\django\db\models\fields\uuuu init\uuuu.py”,
第957行,在get_db_prep_值中
value=connection.ops.validate\u autopk\u值(value)
文件“D:\software\WinPython-64bit-3.5.4.0Qt5\python-3.5.4.amd64\lib\site packages\django\db\backends\mysql\operations.py”,第172行,在validate\u autopk\u值中
raise VALUERROR('数据库后端不接受0作为'
ValueError:数据库后端不接受0作为AutoField的值
数据库无法接受
pk
,foreign key
值为0
。但是您尝试将0
分配为foreign key字段的值。因此,出现了错误
要解决此问题,请为pk
或foreign key
字段类型int
,big int
指定值>0。
还补充说,如果您使用
UUID
作为您的pk
,那么您也不能将0
指定为值。数据库不能接受pk
,外键
值作为0
。但是您尝试将0
指定为外键字段的值。因此,出现了错误
要解决此问题,请为pk
或foreign key
字段类型int
,big int
指定值>0。
还补充说,如果您使用
UUID
作为您的pk
,那么您也不能将0
指定为值。您将notecategory id设置为0?请向我们显示完整的错误回溯。谢谢;我已经编辑了