Forums

ValueError: invalid literal for int() with base 10:

I'm getting an error during a migration.

int() is expected, but it is getting a string 'tom'. I can not figure out where that string is coming from, or how to change it. I've attempted to look at all my models, the files in the error report, etc.

Any hints as to what I might do, or what resources I could read would be tremendously appreciated! Thank you for any help!

I suspect that it has something to do with an old migration where I changed an attribute to on_delete=PROTECT. I now have all models set to on_delete=CASCADE. But, I can't get past this error.

FULL MESSAGE:

(django2) 17:31 ~/mysite (master)$ python manage.py migrate                                                                                            
Operations to perform:
  Apply all migrations: admin, auth, blog, contenttypes, forecasts, groups, polls, questions, scores, sessions, users
Running migrations:
  Applying groups.0004_auto_20191008_1617...Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/home/tliptay/.virtualenvs/django2/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/home/tliptay/.virtualenvs/django2/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/tliptay/.virtualenvs/django2/lib/python3.6/site-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/tliptay/.virtualenvs/django2/lib/python3.6/site-packages/django/core/management/base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "/home/tliptay/.virtualenvs/django2/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/home/tliptay/.virtualenvs/django2/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 234, in handle
    fake_initial=fake_initial,
  File "/home/tliptay/.virtualenvs/django2/lib/python3.6/site-packages/django/db/migrations/executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/home/tliptay/.virtualenvs/django2/lib/python3.6/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/home/tliptay/.virtualenvs/django2/lib/python3.6/site-packages/django/db/migrations/executor.py", line 245, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/tliptay/.virtualenvs/django2/lib/python3.6/site-packages/django/db/migrations/migration.py", line 124, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/home/tliptay/.virtualenvs/django2/lib/python3.6/site-packages/django/db/migrations/operations/fields.py", line 249, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/home/tliptay/.virtualenvs/django2/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 535, in alter_field
    old_db_params, new_db_params, strict)
  File "/home/tliptay/.virtualenvs/django2/lib/python3.6/site-packages/django/db/backends/postgresql/schema.py", line 124, in _alter_field
    new_db_params, strict,
  File "/home/tliptay/.virtualenvs/django2/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 648, in _alter_field
    old_default = self.effective_default(old_field)
  File "/home/tliptay/.virtualenvs/django2/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 233, in effective_default
    return field.get_db_prep_save(self._effective_default(field), self.connection)
  File "/home/tliptay/.virtualenvs/django2/lib/python3.6/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 "/home/tliptay/.virtualenvs/django2/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 789, in get_db_prep_save
    return self.get_db_prep_value(value, connection=connection, prepared=False)
  File "/home/tliptay/.virtualenvs/django2/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 957, in get_db_prep_value
    value = self.get_prep_value(value)
  File "/home/tliptay/.virtualenvs/django2/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 966, in get_prep_value
    return int(value)
ValueError: invalid literal for int() with base 10: 'tom'

[edit by admin: formatting]

That's an odd one! What are the contents of the migration file for 0004_auto_20191008_1617? (If there's anything private in there, you can send it to us using the "Send feedback" link above.)

Below is the file 0004_auto_20191008_1617.py

# Generated by Django 2.2.3 on 2019-10-08 20:17

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


class Migration(migrations.Migration):

    dependencies = [
        ('groups', '0003_auto_20191008_1601'),
    ]

    operations = [
        migrations.AlterField(
            model_name='group',
            name='owner',
            field=models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, to=settings.AUTH_USER_MODEL),
        ),
    ]

[edit by admin: formatting]

Was the groups.Group model one of the ones where you changed a field (specifically, the owner field) from being on_delete=PROTECT to on_delete=CASCADE? What's the definition of that field in your models.py?

Below is the Group model.

I should add that I'm a newbie and I wouldn't be surprised if during a migration django asked for a default value for owner and I entered 'tom' - assuming that django would throw an error if that was an invalid entry. However, when I look at the SQLite db I don't see any owner 'tom'.

Basically, I can't find 'tom' located in any of my code and can't figure out how to debug.

class Group(models.Model):
    name = models.CharField(max_length=16, validators=[validators.validate_slug])
    owner = models.ForeignKey(User, on_delete=models.CASCADE)
    public = models.BooleanField(default=False)
    pin = models.IntegerField(validators=[MinValueValidator(100000000),
        MaxValueValidator(999999999)], unique=True)
    members = models.ManyToManyField(User, related_name='members', blank=True)

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return reverse('groups-home')

[edit by admin: formatting]

Thanks! From your original stacktrace, it looks like the problem is coming from the previous version of the class:

old_default = self.effective_default(old_field)

If I'm reading the Django code correctly, it's asking the old (pre-migration) version of the class what its default was, and (as you suspected) getting "tom" back. If you grep your migrations directory for the string "tom", do you get any hits?

Found it! Thank you so much!

Of course, I'm not sure how to fix this - but I can bang my head on this for a while now that I understand what happened.

When I first created model groups I had the default set to "tom", simply because I had no idea what I was doing. I guess I'm a little surprised that I was able to continue for 2 months without this cropping up or causing any problems.

I agree, it's pretty surprising it ever worked! You might be able to get somewhere by digging around in your database (using a MySQL or Postgres console, or the sqlite3 bash command as appropriate depending on which database you're using) to see if the problem is a "tom" appearing somewhere in the database itself.

Hi guys,

Can you help, I am getting the following error during the migrations: ValueError: invalid literal for int() with base 10: 'abc'

(django2) 19:32 ~/mysite $ python manage.py migrate
/home/RichoM/.virtualenvs/django2/lib/python3.6/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
  """)
Operations to perform:
  Apply all migrations: admin, auth, blog, contenttypes, sessions, sites, taggit
Running migrations:
  Applying blog.0004_auto_20200821_0127...Traceback (most recent call last):
  File "/home/RichoM/.virtualenvs/django2/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 1772, in get_prep_value
    return int(value)
ValueError: invalid literal for int() with base 10: 'abc'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/home/RichoM/.virtualenvs/django2/lib/python3.6/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/home/RichoM/.virtualenvs/django2/lib/python3.6/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/RichoM/.virtualenvs/django2/lib/python3.6/site-packages/django/core/management/base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/RichoM/.virtualenvs/django2/lib/python3.6/site-packages/django/core/management/base.py", line 369, in execute
    output = self.handle(*args, **options)
  File "/home/RichoM/.virtualenvs/django2/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/home/RichoM/.virtualenvs/django2/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 233, in handle
    fake_initial=fake_initial,
  File "/home/RichoM/.virtualenvs/django2/lib/python3.6/site-packages/django/db/migrations/executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/home/RichoM/.virtualenvs/django2/lib/python3.6/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/home/RichoM/.virtualenvs/django2/lib/python3.6/site-packages/django/db/migrations/executor.py", line 245, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/RichoM/.virtualenvs/django2/lib/python3.6/site-packages/django/db/migrations/migration.py", line 124, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/home/RichoM/.virtualenvs/django2/lib/python3.6/site-packages/django/db/migrations/operations/fields.py", line 249, in database_forw
ards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/home/RichoM/.virtualenvs/django2/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 565, in alter_field
    old_db_params, new_db_params, strict)
  File "/home/RichoM/.virtualenvs/django2/lib/python3.6/site-packages/django/db/backends/postgresql/schema.py", line 154, in _alter_field
    new_db_params, strict,
  File "/home/RichoM/.virtualenvs/django2/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 678, in _alter_field
    old_default = self.effective_default(old_field)
  File "/home/RichoM/.virtualenvs/django2/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 303, in effective_default
    return field.get_db_prep_save(self._effective_default(field), self.connection)
  File "/home/RichoM/.virtualenvs/django2/lib/python3.6/site-packages/django/db/models/fields/related.py", line 939, in get_db_prep_save
    return self.target_field.get_db_prep_save(value, connection=connection)
  File "/home/RichoM/.virtualenvs/django2/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 821, in get_db_prep_save
    return self.get_db_prep_value(value, connection=connection, prepared=False)
  File "/home/RichoM/.virtualenvs/django2/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 2365, in get_db_prep_value
    value = self.get_prep_value(value)
  File "/home/RichoM/.virtualenvs/django2/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 1776, in get_prep_value
    ) from e
ValueError: Field 'id' expected a number but got 'abc'.
(django2) 19:32 ~/mysite $

[edit by admin: formatting]

It looks like you've provided "abc" as the value for your id column somehow. What code do you have in the models.py for your blog Django app?

Hi,

This is the code in the models.py

from django.db import models
from django.utils import timezone
from django.urls import reverse
from django.contrib.auth.models import User
from taggit.managers import TaggableManager


class PublishedManager(models.Manager):
    def get_queryset(self):
        return super().get_queryset().filter(status='published')


class Post(models.Model):
    STATUS_CHOICES = (
        ('draft', 'Draft'),
        ('published', 'Published'),
    )
    title = models.CharField(max_length=250)
    slug = models.SlugField(max_length=250,
                            unique_for_date='publish')
    author = models.ForeignKey(User,
                              on_delete=models.CASCADE,
                              related_name='blog_posts')
    body = models.TextField()
    publish = models.DateTimeField(default=timezone.now)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    status = models.CharField(max_length=10,
                              choices=STATUS_CHOICES,
                              default='draft')

    objects = models.Manager() # The default manager.
    published = PublishedManager() # Our custom manager.
    tags = TaggableManager()

    class Meta:
        ordering = ('-publish',)

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('blog:post_detail',
                       args=[self.publish.year,
                             self.publish.month,
                             self.publish.day, self.slug])


class Comment(models.Model):
    post = models.ForeignKey(Post,
                             on_delete=models.CASCADE,
                             related_name='comments')
    name = models.CharField(max_length=80)
    email = models.EmailField()
    body = models.TextField()
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    active = models.BooleanField(default=True)

    class Meta:
        ordering = ('created',)

    def __str__(self):
        return f'Comment by {self.name} on {self.post}'

[edit by admin: formatting]

Thanks! I don't see anything obviously wrong there. What are the contents of the file the the name starting 0004_auto_20200821_0127 in the migrations subdirectory of your polls directory?

0004_auto_20200821_0127.py:

# Generated by Django 3.0.5 on 2020-08-21 01:27

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


class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('blog', '0003_post_tags'),
    ]

    operations = [
        migrations.AlterField(
            model_name='post',
            name='author',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='blog_posts', to=settings.AUTH_USER_MODEL),
        ),
        migrations.AlterField(
            model_name='post',
            name='body',
            field=models.TextField(),
        ),
    ]

[edit by admin: formatting]

I think the issue was caused by changing the Database to Postgres.

How was your data migrated between databases?

I migrated by the following command:

python manage.py migrate

but after doing the first migration, I made some changes and I tried to do migration again which was now failing.

Do you have any rows in your post table where the author column contains the value "abc"?

No, I don't have a column containing the value "abc" https://ibb.co/1TWK58V

That image link isn't working -- I'm getting "The requested page was not found." Could you try posting again?

enter image description here

Find the url for the image

https://ibb.co/VxcHmqP

That shows the databases that you've created; what I'm asking is if you have a row in the post table that has particular data inside it. You'd need to run a select query on the appropriate table to determine that.

Problem solved by installing the following extension: pg_trgm;

The extension was not installed.

Thank you for the effort.

OK, glad you worked it out!