Skip to content
Advertisement

ValueError: Field ‘id’ expected a number but got ‘Die erste Meldung’. after adding an item via console

I’m trying to add an item into db.sqlite3 via command line

In [10]: m = Meldung('Die erste Meldung', zeitstempel=datetime.today(), text='Nun kommt das klassische Hallo-Welt-Beispiel.')

In [11]: m.save()

but got this error:

ValueError: Field 'id' expected a number but got 'Die erste Meldung'.

Inside migrations001_initial.py below the field id of contentobject Meldung is declared as auto_created=True, but it seems it’s not working as expected.

How can I solve it?

  • expected result
    Field ‘id’ is generated automated by db.sqlite3

  • environment
    Python 3.8.2
    Django 3.1.3

  • models.py

from django.db import models




class Meldung(models.Model):
    titel = models.CharField(max_length=100)
    zeitstempel = models.DateTimeField()
    text = models.TextField('Meldungstext')


class Kommentar(models.Model):
    meldung = models.ForeignKey(
        Meldung,
        on_delete=models.CASCADE,
    )
    autor = models.CharField(max_length=70)
    text = models.TextField('Kommentartext')
  • migrations001_initial.py
# Generated by Django 3.1.3 on 2020-11-04 20:57


from django.db import migrations, models
import django.db.models.deletion




class Migration(migrations.Migration):


    initial = True


    dependencies = [
    ]


    operations = [
        migrations.CreateModel(
            name='Meldung',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('titel', models.CharField(max_length=100)),
                ('zeitstempel', models.DateTimeField()),
                ('text', models.TextField(verbose_name='Meldungstext')),
            ],
        ),
        migrations.CreateModel(
            name='Kommentar',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('autor', models.CharField(max_length=70)),
                ('text', models.TextField(verbose_name='Kommentartext')),
                ('meldung', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='news.meldung')),
            ],
        ),
    ]

Advertisement

Answer

The first parameter if you pass the value positionally (so without the name of the parameter), is id, so you assign 'Die erste Meldung' not to title but to the implicit id field.

You can solve this by passing it as a named parameter:

m = Meldung(
    title='Die erste Meldung',
    zeitstempel=datetime.today(),
    text='Nun kommt das klassische Hallo-Welt-Beispiel.'
)

you can also pass None to the id parameter:

m = Meldung(
    None,
    'Die erste Meldung',
    zeitstempel=datetime.today(),
    text='Nun kommt das klassische Hallo-Welt-Beispiel.'
)

but it makes it unclear what each value represents in the Meldung object, as you found out yourself.

Advertisement