简体   繁体   中英

Django `migrate` command fails with: ValueError: invalid literal for int() with base 10: '' "IntegerField"

Can you explain it to me? Why this is my models.py? I get this error message: ValueError: invalid literal for int() with base 10: ''

from django.db import models

# Create your models here.

class Contact(models.Model):
    first_name = models.CharField(
        max_length=255,
    )
    last_name = models.CharField(
        max_length=255,
    )
    email = models.EmailField(
        max_length=255,
    )
    nomor_hp = models.IntegerField(

    )

I suspect what happened is this. You added the nomor_hp field to your Contact model. Then you ran makemigrations and got output that looked something like:

You are trying to add a non-nullable field 'num' to foo without a default; we can't do that (the database needs something to populate existing rows).
Please select a fix:
 1) Provide a one-off default now (will be set on all existing rows with a null value for this column)
 2) Quit, and let me add a default in models.py
Select an option: 1
Please enter the default value now, as valid Python
The datetime and django.utils.timezone modules are available, so you can do e.g. timezone.now
Type 'exit' to exit this prompt

You probably selected 1 (like above) and just hit enter or '' . Since '' is not an integer the migrate fails with the message you posted. The thing to do is:

1) Fix your Contact.nomor_hp field to this nomor_hp = models.IntegerField(default=None)

2) Run makemigrations again

3) Edit the offending migrations file (you can find it in the trace output) and in your case it is the 0003_contact_nomor_hp.py file. In that file you should change …

   operations = [
        migrations.AddField(
            model_name='contact',
            name='nomor_hp',
            field=models.IntegerField(default=''),
            preserve_default=False,
        ),
    ]

… to …

   operations = [
        migrations.AddField(
            model_name='contact',
            name='nomor_hp',
            field=models.IntegerField(default=0),
            preserve_default=False,
        ),
    ]

4) Run migrate

Then you should be good.

You're trying to convert a string to an integer via int() somewhere in your models.IntegerField and this is failing because int() does not know what to do with a string.

To solve this, ensure your data inputs are all integers or numbers.

You have posted the following log in the comments:

>python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contacts, contenttypes, sessions
Running migrations:
  Applying contacts.0003_contact_nomor_hp...Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 316, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 353, in execute
    output = self.handle(*args, **options)
  File "C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\management\commands\migrate.py", line 203, in handle
    fake_initial=fake_initial,
  File "C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\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 "C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\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 "C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\migration.py", line 124, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\migrations\operations\fields.py", line 84, in database_forwards
    field,
  File "C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\mysql\schema.py", line 42, in add_field
    super().add_field(model, field)
  File "C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\schema.py", line 421, in add_field
    definition, params = self.column_sql(model, field, include_default=True)
  File "C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\schema.py", line 168, in column_sql
    default_value = self.effective_default(field)
  File "C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\backends\base\schema.py", line 239, in effective_default
    return field.get_db_prep_save(default, self.connection)
  File "C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\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 "C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\fields\__init__.py", line 785, in get_db_prep_value
    value = self.get_prep_value(value)
  File "C:\Users\Heri Prastio\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\db\models\fields\__init__.py", line 1807, in get_prep_value
    return int(value)
ValueError: invalid literal for int() with base 10: ''

The problem is probably that you already have some data in the database, therefore, you are not able to add the column nomor_hp .

You have 2 options here:

  1. Add a default value to the filed ( IntegerField(default=0) )
  2. Make the field nullable ( IntegerField(null=True, blank=True) )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM