Opened 5 years ago

Closed 5 years ago

#30293 closed Bug (invalid)

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

Reported by: Bob Tanner Owned by: nobody
Component: Migrations Version: 2.1
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Added the face_png foreign key and not migrate fails with ValueError: invalid literal for int() with base 10: 'None, remove the foreign key from ArchBase and migrate works as expected.

class FacePng(models.Model):
    arc_filename = models.CharField(max_length=128, blank=True, null=True)
    png = models.CharField(max_length=128, blank=True, null=True)

    def __str__(self):
        return self.png
class ArchBase(models.Model):
    face_png = models.ForeignKey(
        'FacePng',
        on_delete=models.CASCADE,
        blank=True,
        null=False,
        default="None",
    )
$ ./manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, items, sessions
Running migrations:
  Applying items.0001_initial...Traceback (most recent call last):
  File "./manage.py", line 24, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/tanner/projects/crossfire/archdb/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/Users/tanner/projects/crossfire/archdb/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/tanner/projects/crossfire/archdb/lib/python3.7/site-packages/django/core/management/base.py", line 316, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/tanner/projects/crossfire/archdb/lib/python3.7/site-packages/django/core/management/base.py", line 353, in execute
    output = self.handle(*args, **options)
  File "/Users/tanner/projects/crossfire/archdb/lib/python3.7/site-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/Users/tanner/projects/crossfire/archdb/lib/python3.7/site-packages/django/core/management/commands/migrate.py", line 203, in handle
    fake_initial=fake_initial,
  File "/Users/tanner/projects/crossfire/archdb/lib/python3.7/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 "/Users/tanner/projects/crossfire/archdb/lib/python3.7/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 "/Users/tanner/projects/crossfire/archdb/lib/python3.7/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/Users/tanner/projects/crossfire/archdb/lib/python3.7/site-packages/django/db/migrations/migration.py", line 124, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/Users/tanner/projects/crossfire/archdb/lib/python3.7/site-packages/django/db/migrations/operations/fields.py", line 84, in database_forwards
    field,
  File "/Users/tanner/projects/crossfire/archdb/lib/python3.7/site-packages/django/db/backends/sqlite3/schema.py", line 309, in add_field
    self._remake_table(model, create_field=field)
  File "/Users/tanner/projects/crossfire/archdb/lib/python3.7/site-packages/django/db/backends/sqlite3/schema.py", line 181, in _remake_table
    self.effective_default(create_field)
  File "/Users/tanner/projects/crossfire/archdb/lib/python3.7/site-packages/django/db/backends/base/schema.py", line 239, in effective_default
    return field.get_db_prep_save(default, self.connection)
  File "/Users/tanner/projects/crossfire/archdb/lib/python3.7/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 "/Users/tanner/projects/crossfire/archdb/lib/python3.7/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 "/Users/tanner/projects/crossfire/archdb/lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 956, in get_db_prep_value
    value = self.get_prep_value(value)
  File "/Users/tanner/projects/crossfire/archdb/lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 965, in get_prep_value
    return int(value)
ValueError: invalid literal for int() with base 10: 'None'

Change History (1)

comment:1 by Simon Charette, 5 years ago

Resolution: invalid
Status: newclosed

Unless ForeignKey.to_field is specified your ArchBase.face_png field will point to the FacePng.id which is the implicit primary key when none is explicitly specified. This field is an auto-incrementing integer hence why int('None') is attempted.

You either want to explicitly declare one your FacePng fields primary key or make one them unique and adjust ArchBase.face_png(to_field) to point to it. Please see one of the support channels if you need further assistance.

Note: See TracTickets for help on using tickets.
Back to Top