简体   繁体   中英

Django migrate value invalid literal

I have problem with Django migrate function. I was trying to add new field to my user model and it looks like this.

class UserProfile(models.Model):
    """ Model to represent additional information about user """
    user = models.OneToOneField(
        User,
        on_delete=models.CASCADE,
        related_name='profile'
    )
    bio = models.TextField(
        max_length=2000,
        blank=True,
        default=''
    )
    # we use URL instead of imagefield because we'll use 3rd party img hosting later on
    avatar = models.URLField(default='', blank=True)
    status = models.CharField(max_length=16, default='', blank=True)
    name = models.CharField(max_length=32, default='')
    balance = models.BigIntegerField(default='0')

    def __str__(self):
        return self.user.username

balance is new what I added, and after that I'm receving messages like

Operations to perform: Apply all migrations: accounts, admin, auth, authtoken, contenttypes, forums, posts, sessions, threads Running migrations: Applying accounts.0005_userprofile_balance...Traceback (most recent call last): File "manage.py", line 15, in execute_from_command_line(sys.argv) File "C:\Users\Rade\Desktop\rengorum-master\backend\env\lib\site-packages\django\core\management__init__.py", line 371, in execute_from_command_line utility.execute() File "C:\Users\Rade\Desktop\rengorum-master\backend\env\lib\site-packages\django\core\management__init__.py", line 365, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "C:\Users\Rade\Desktop\rengorum-master\backend\env\lib\site-packages\django\core\management\base.py", line 288, in run_from_argv self.execute(*args, **cmd_options) File "C:\Users\Rade\Desktop\rengorum-master\backend\env\lib\site-packages\django\core\management\base.py", line 335, in execute output = self.handle(*args, * *options) File "C:\Users\Rade\Desktop\rengorum-master\backend\env\lib\site-packages\django\core\management\commands\migrate.py", line 200, in handle fake_initial=fake_initial, File "C:\Users\Rade\Desktop\rengorum-master\backend\env\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\Rade\Desktop\rengorum-master\backend\env\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\Rade\Desktop\rengorum-master\backend\env\lib\site-packages\django\db\migrations\executor.py", line 244, in apply_migration state = migration.apply(state, schema_editor) File "C:\Users\Rade\Desktop\rengorum-master\backend\env\lib\site-packages\django\db\migrations\mi gration.py", line 122, in apply operation.database_forwards(self.app_label, schema_editor, old_state, project_state) File "C:\Users\Rade\Desktop\rengorum-master\backend\env\lib\site-packages\django\db\migrations\operations\fields.py", line 84, in database_forwards field, File "C:\Users\Rade\Desktop\rengorum-master\backend\env\lib\site-packages\django\db\backends\sqlite3\schema.py", line 315, in add_field self._remake_table(model, create_field=field) File "C:\Users\Rade\Desktop\rengorum-master\backend\env\lib\site-packages\django\db\backends\sqlite3\schema.py", line 187, in _remake_table self.effective_default(create_field) File "C:\Users\Rade\Desktop\rengorum-master\backend\env\lib\site-packages\django\db\backends\base\schema.py", line 240, in effective_default default = field.get_db_prep_save(default, self.connection) File "C:\Users\Rade\Desktop\rengorum-master\backend\env\lib\site-packages\django\db\models\fields__init__.py", line 767, in get_db_prep_save return self.get_db_prep_valu e(value, connection=connection, prepared=False) File "C:\Users\Rade\Desktop\rengorum-master\backend\env\lib\site-packages\django\db\models\fields__init__.py", line 762, in get_db_prep_value value = self.get_prep_value(value) File "C:\Users\Rade\Desktop\rengorum-master\backend\env\lib\site-packages\django\db\models\fields__init__.py", line 1826, in get_prep_value return int(value) ValueError: invalid literal for int() with base 10: ''

after I received that message, I tried to set default value to 0.00, but it's still same message, I deleted balance field and still receiving same message. Any ideas?

This is basically a migration issue, as the migration files are generated based on your initial codes and when migrating, these migration files are being executed and throwing errors.

I think you can use either one of the following solution to fix it:

Solution One

First you should delete the migration file(ie 0003_auto_<some id>.py ) inside <app>/migrations directory which was created when you ran python manage.py makemigrations . If you are unsure about which one to delete, then check the django_migrations table in your database and see till which migrations it has been applied. After removing the last migration file(or files), you need to change the default value to 0 . Then you should run makemigrations and migrate commands.

Solution Two

Change the default value to 0 in models. Then change the migration file which has been created when you added the Field in model, like this:

    operations = [
        migrations.AddField(
            model_name='userprofile',
            name='balance',
            field=models.BigIntegerField(default=0),  # instead of default='0'
        ),

Also you should remove the additional migration files(if they are generated) when you fixed the default value and re ran the migration commands.

BigIntegerField default value needs to be a whole number and not in quotes:

balance = models.BigIntegerField(default=0)

alternatively, if you need decimals:

balance = models.DecimalField(max_digits=6, decimal_places=2, default=0)

or use MoneyField https://github.com/django-money/django-money

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