Search code examples
pythondjangodatabase-migration

Invalid literal for int() error in ForeignKey default


My models.py:

class orientation_type(models.Model):
    nome = models.CharField(max_length=16, unique=True)
    def __str__(self):
        return self.nome

class region(models.Model):
    nome = models.CharField(max_length=16, unique=True)
    def __str__(self):
        return self.nome

class province(models.Model):
    city = models.OneToOneField(community, blank= True, null=True)
    orientation = models.ForeignKey(orientation_type, default='generic')
    region = models.ForeignKey(region, default='north')

In my first migration (so I don't have definite the regions yet and the database don't exist) it give this error:

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

Of course I don't want an integer, why it look for one? And why it don't give the same error for orientation too (if I delete default='north' it works)?

This is the full error:

manage.py migrate

Operations to perform:
  Apply all migrations: admin, sessions, core, auth, contenttypes
Running migrations:
  Rendering model states... DONE
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying core.0001_initial...Traceback (most recent call last):
  File "C:\Python34\Scripts\possedimenti\sitopossedimenti\manage.py", line 10, i
n <module>
    execute_from_command_line(sys.argv)
  File "c:\Python34\Scripts\possedimenti\myvenv\lib\site-packages\django\core\ma
nagement\__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "c:\Python34\Scripts\possedimenti\myvenv\lib\site-packages\django\core\ma
nagement\__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "c:\Python34\Scripts\possedimenti\myvenv\lib\site-packages\django\core\ma
nagement\base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "c:\Python34\Scripts\possedimenti\myvenv\lib\site-packages\django\core\ma
nagement\base.py", line 399, in execute
    output = self.handle(*args, **options)
  File "c:\Python34\Scripts\possedimenti\myvenv\lib\site-packages\django\core\ma
nagement\commands\migrate.py", line 200, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "c:\Python34\Scripts\possedimenti\myvenv\lib\site-packages\django\db\migr
ations\executor.py", line 92, in migrate
    self._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_ini
tial)
  File "c:\Python34\Scripts\possedimenti\myvenv\lib\site-packages\django\db\migr
ations\executor.py", line 121, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_
initial)
  File "c:\Python34\Scripts\possedimenti\myvenv\lib\site-packages\django\db\migr
ations\executor.py", line 198, in apply_migration
    state = migration.apply(state, schema_editor)
  File "c:\Python34\Scripts\possedimenti\myvenv\lib\site-packages\django\db\migr
ations\migration.py", line 123, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, projec
t_state)
  File "c:\Python34\Scripts\possedimenti\myvenv\lib\site-packages\django\db\migr
ations\operations\fields.py", line 62, in database_forwards
    field,
  File "c:\Python34\Scripts\possedimenti\myvenv\lib\site-packages\django\db\back
ends\sqlite3\schema.py", line 221, in add_field
    self._remake_table(model, create_fields=[field])
  File "c:\Python34\Scripts\possedimenti\myvenv\lib\site-packages\django\db\back
ends\sqlite3\schema.py", line 103, in _remake_table
    self.effective_default(field)
  File "c:\Python34\Scripts\possedimenti\myvenv\lib\site-packages\django\db\back
ends\base\schema.py", line 210, in effective_default
    default = field.get_db_prep_save(default, self.connection)
  File "c:\Python34\Scripts\possedimenti\myvenv\lib\site-packages\django\db\mode
ls\fields\related.py", line 912, in get_db_prep_save
    return self.target_field.get_db_prep_save(value, connection=connection)
  File "c:\Python34\Scripts\possedimenti\myvenv\lib\site-packages\django\db\mode
ls\fields\__init__.py", line 728, in get_db_prep_save
    prepared=False)
  File "c:\Python34\Scripts\possedimenti\myvenv\lib\site-packages\django\db\mode
ls\fields\__init__.py", line 968, in get_db_prep_value
    value = self.get_prep_value(value)
  File "c:\Python34\Scripts\possedimenti\myvenv\lib\site-packages\django\db\mode
ls\fields\__init__.py", line 976, in get_prep_value
    return int(value)
ValueError: invalid literal for int() with base 10: 'north'

Solution

  • As noted in the comments, ForeginKeys are expected to be the primary keys (e.g. an integer), or an instance of the model.

    However I believe you are running into a deeper issue with ForeignKey default values in Django.

    Because that value needs to be available to Django when the model is loaded.

    You're not going to be able to make a Region query, get the object's primary key and use that as the default foreign key, and that value will need to be available for the migration script too, which are incompatible with lambda functions that you may cook up to get the foreign key value.

    More on the issues on this topic in this question.

    If you really need a default value for that foreign key, I suggest you use a fixture with the Region object, remember it's primary key, then hardcode that into the foreign key value

    e.g. a fixture like this

    model: app.Region
    pk: 1
    fields:
        name: 'North'
    

    then use:

    region = models.ForeignKey(Region, default=1)