django 2 mysql数据库后端不接受0作为自动字段错误的值

时间:2019-01-18 11:30:26

标签: 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_autopk_value中       引发ValueError('数据库后端不接受0作为'ValueError:数据库后端不接受0作为   自动字段。

在我的note / models.py

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')

在我的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()

在note / 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行,在内部           响应= get_response(请求)         文件“ D:\ software \ WinPython-64bit-3.5.4.0Qt5 \ python-3.5.4.amd64 \ lib \ site-packages \ django \ core \ handlers \ base.py”,   _get_response中的第126行           响应= self.process_exception_by_middleware(e,请求)         文件“ D:\ software \ WinPython-64bit-3.5.4.0Qt5 \ python-3.5.4.amd64 \ lib \ site-packages \ django \ core \ handlers \ base.py”,   _get_response中的第124行           响应= wraped_callback(请求,* callback_args,** callback_kwargs)         在savenote中的文件“ D:\ pythonworkspace \ notetaking \ read \ views.py”,第143行           toSave.save()         保存文件``D:\ software \ WinPython-64bit-3.5.4.0Qt5 \ python-3.5.4.amd64 \ lib \ site-packages \ django \ db \ models \ base.py'',行717,保存           force_update = force_update,update_fields = update_fields)         在save_base中的第747行的文件“ D:\ software \ WinPython-64bit-3.5.4.0Qt5 \ python-3.5.4.amd64 \ lib \ site-packages \ django \ db \ models \ base.py”           更新= self._save_table(原始,cls,force_insert,force_update,使用,update_fields)         _save_table中的第830行的文件“ D:\ software \ WinPython-64bit-3.5.4.0Qt5 \ python-3.5.4.amd64 \ lib \ site-packages \ django \ db \ models \ base.py”           结果= self._do_insert(cls._base_manager,使用字段,update_pk,原始)         _do_insert中的第868行的文件“ D:\ software \ WinPython-64bit-3.5.4.0Qt5 \ python-3.5.4.amd64 \ lib \ site-packages \ django \ db \ models \ base.py”           使用=使用,原始=原始)         文件“ D:\ software \ WinPython-64bit-3.5.4.0Qt5 \ python-3.5.4.amd64 \ lib \ site-packages \ django \ db \ models \ manager.py”,   第82行,在manager_method中           返回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”,   _insert中的第1133行           返回query.get_compiler(using = using).execute_sql(return_id)         文件“ D:\ software \ WinPython-64bit-3.5.4.0Qt5 \ python-3.5.4.amd64 \ lib \ site-packages \ django \ db \ models \ sql \ compiler.py”,   第1284行,在execute_sql中           对于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_value(field,self.pre_save_val(field,obj))]         文件“ D:\ software \ WinPython-64bit-3.5.4.0Qt5 \ python-3.5.4.amd64 \ lib \ site-packages \ django \ db \ models \ sql \ compiler.py”,   第1178行,在prepare_value中           值= field.get_db_prep_save(值,连接=自连接)         文件“ 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_field.get_db_prep_save(value,connection = connection)         文件“ D:\ software \ WinPython-64bit-3.5.4.0Qt5 \ python-3.5.4.amd64 \ lib \ site-packages \ django \ db \ models \ fields__init __。py”,   第790行,在get_db_prep_save中           返回self.get_db_prep_value(值,连接=连接,准备=假)         文件“ D:\ software \ WinPython-64bit-3.5.4.0Qt5 \ python-3.5.4.amd64 \ lib \ site-packages \ django \ db \ models \ fields__init __。py”,   第957行,位于get_db_prep_value中           值= connection.ops.validate_autopk_value(值)         validate_autopk_value中的第172行的文件“ D:\ software \ WinPython-64bit-3.5.4.0Qt5 \ python-3.5.4.amd64 \ lib \ site-packages \ django \ db \ backends \ mysql \ operations.py”           引发ValueError('数据库后端不接受0作为'       ValueError:数据库后端不接受0作为AutoField的值。

1 个答案:

答案 0 :(得分:1)

数据库无法将pkforeign key的值接受为0。但是,您尝试将0分配为外键字段的值。因此,出现错误。

要解决此问题,请为pkforeign key字段类型intbig int分配> 0的值。 还补充一点,如果您将UUID用作pk,则也不能将0赋值。