0
votes

I don't know why: the makemigrations command works fine but when I insert something from the browser, the terminal outputs error message...

File "D:\software\WinPython-64bit-3.5.4.0Qt5\python-3.5.4.amd64\lib\site-packages\django\db\backends\mysql\operations.py", line 172, in validate_autopk_value raise ValueError('The database backend does not accept 0 as a ' ValueError: The database backend does not accept 0 as a value for AutoField.

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

in 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()

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

full track I paste the error from another similar model/app, because I didn't copy the original message

[18/Jan/2019 19:46:47] "GET /readbook/? HTTP/1.1" 200 5809 Internal Server Error: /savenote/ Traceback (most recent call last): File "D:\software\WinPython-64bit-3.5.4.0Qt5\python-3.5.4.amd64\lib\site-packages\django\core\handlers\exception.py", line 34, in inner response = get_response(request) File "D:\software\WinPython-64bit-3.5.4.0Qt5\python-3.5.4.amd64\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response response = self.process_exception_by_middleware(e, request) File "D:\software\WinPython-64bit-3.5.4.0Qt5\python-3.5.4.amd64\lib\site-packages\django\core\handlers\base.py", line 124, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "D:\pythonworkspace\notetaking\read\views.py", line 143, in savenote toSave.save() File "D:\software\WinPython-64bit-3.5.4.0Qt5\python-3.5.4.amd64\lib\site-packages\django\db\models\base.py", line 717, in save force_update=force_update, update_fields=update_fields) File "D:\software\WinPython-64bit-3.5.4.0Qt5\python-3.5.4.amd64\lib\site-packages\django\db\models\base.py", line 747, in save_base updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields) File "D:\software\WinPython-64bit-3.5.4.0Qt5\python-3.5.4.amd64\lib\site-packages\django\db\models\base.py", line 830, in _save_table result = self._do_insert(cls._base_manager, using, fields, update_pk, raw) File "D:\software\WinPython-64bit-3.5.4.0Qt5\python-3.5.4.amd64\lib\site-packages\django\db\models\base.py", line 868, in _do_insert using=using, raw=raw) File "D:\software\WinPython-64bit-3.5.4.0Qt5\python-3.5.4.amd64\lib\site-packages\django\db\models\manager.py", line 82, in manager_method return getattr(self.get_queryset(), name)(*args, **kwargs) File "D:\software\WinPython-64bit-3.5.4.0Qt5\python-3.5.4.amd64\lib\site-packages\django\db\models\query.py", line 1133, in _insert return query.get_compiler(using=using).execute_sql(return_id) File "D:\software\WinPython-64bit-3.5.4.0Qt5\python-3.5.4.amd64\lib\site-packages\django\db\models\sql\compiler.py", line 1284, in execute_sql for sql, params in self.as_sql(): File "D:\software\WinPython-64bit-3.5.4.0Qt5\python-3.5.4.amd64\lib\site-packages\django\db\models\sql\compiler.py", line 1237, in as_sql for obj in self.query.objs File "D:\software\WinPython-64bit-3.5.4.0Qt5\python-3.5.4.amd64\lib\site-packages\django\db\models\sql\compiler.py", line 1237, in for obj in self.query.objs File "D:\software\WinPython-64bit-3.5.4.0Qt5\python-3.5.4.amd64\lib\site-packages\django\db\models\sql\compiler.py", line 1236, in [self.prepare_value(field, self.pre_save_val(field, obj)) for field in fields] File "D:\software\WinPython-64bit-3.5.4.0Qt5\python-3.5.4.amd64\lib\site-packages\django\db\models\sql\compiler.py", line 1178, in prepare_value value = field.get_db_prep_save(value, connection=self.connection) File "D:\software\WinPython-64bit-3.5.4.0Qt5\python-3.5.4.amd64\lib\site-packages\django\db\models\fields\related.py", line 937, in get_db_prep_save return self.target_field.get_db_prep_save(value, connection=connection) File "D:\software\WinPython-64bit-3.5.4.0Qt5\python-3.5.4.amd64\lib\site-packages\django\db\models\fields__init__.py", line 790, in get_db_prep_save return self.get_db_prep_value(value, connection=connection, prepared=False) File "D:\software\WinPython-64bit-3.5.4.0Qt5\python-3.5.4.amd64\lib\site-packages\django\db\models\fields__init__.py", line 957, in get_db_prep_value value = connection.ops.validate_autopk_value(value) File "D:\software\WinPython-64bit-3.5.4.0Qt5\python-3.5.4.amd64\lib\site-packages\django\db\backends\mysql\operations.py", line 172, in validate_autopk_value raise ValueError('The database backend does not accept 0 as a ' ValueError: The database backend does not accept 0 as a value for AutoField.

1
you set notecategory id to 0?shafik
Please show us the full error traceback.Klaus D.
Thank you; I've edited. I paste the error from another similar model/app, because I didn't copy the original message. I don't know how to make the stackoverflow format nicely.Cindy
@ShafikurRahman notecategory id starts from 1. I found that my sentence_id is assigned to 0. would this matter?Cindy
yes. sentence_id foreign key and it doesn't 0shafik

1 Answers

2
votes

Database can't accept pk, foreign key value as 0. But you try to assign 0 as a value of foreign key field. So, the error arise.

To fix this, assign value > 0 for pk or foreign key field type int, big int. Also added that, if you are using UUID as your pk, then you can't also assign 0 as value.