Forums

migrations issue

I have an existing database and everything was working fine until this last migration. the migration ran fine locally but it won't go through here. Not sure what to do:

Operations to perform:
  Apply all migrations: account, accounts, admin, auth, contenttypes, dashboard, financing, sessions, sites, socialaccount, waterfall
Running migrations:
  Applying dashboard.0009_auto_20220110_1351...Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/home/jsolis/.virtualenvs/UWapp/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/home/jsolis/.virtualenvs/UWapp/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/jsolis/.virtualenvs/UWapp/lib/python3.6/site-packages/django/core/management/base.py", line 316, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/jsolis/.virtualenvs/UWapp/lib/python3.6/site-packages/django/core/management/base.py", line 353, in execute
    output = self.handle(*args, **options)
  File "/home/jsolis/.virtualenvs/UWapp/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/home/jsolis/.virtualenvs/UWapp/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 203, in handle
    fake_initial=fake_initial,
  File "/home/jsolis/.virtualenvs/UWapp/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/jsolis/.virtualenvs/UWapp/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/jsolis/.virtualenvs/UWapp/lib/python3.6/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/jsolis/.virtualenvs/UWapp/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/jsolis/.virtualenvs/UWapp/lib/python3.6/site-packages/django/db/migrations/operations/fields.py", line 216, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/home/jsolis/.virtualenvs/UWapp/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 523, in alter_field
    old_db_params, new_db_params, strict)
  File "/home/jsolis/.virtualenvs/UWapp/lib/python3.6/site-packages/django/db/backends/postgresql/schema.py", line 122, in _alter_field
    new_db_params, strict,
  File "/home/jsolis/.virtualenvs/UWapp/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 626, in _alter_field
    old_default = self.effective_default(old_field)
  File "/home/jsolis/.virtualenvs/UWapp/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 239, in effective_default
    return field.get_db_prep_save(default, self.connection)
  File "/home/jsolis/.virtualenvs/UWapp/lib/python3.6/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 "/home/jsolis/.virtualenvs/UWapp/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 785, in get_db_prep_value
    value = self.get_prep_value(value)
  File "/home/jsolis/.virtualenvs/UWapp/lib/python3.6/site-packages/django/db/models/fields/__init__.py", line 1737, in get_prep_value
    return float(value)
ValueError: could not convert string to float:

The error basically says that there's somewhere a string where db expects a float. Did you run makemigrations before? Do you have the same db locally as on PythonAnywhere?

I did not run makemigrations on python anywhere but I did locally. I had been doing that in all previous migrations.

The db is SQLite locally with the same structure. Pythonanywhere is Postgres

Should I run makemigrations on python anywhere or would that break it?

That looks like you are trying to change a string field to a float and the string field has a value that does not convert to a float. If you're changing the type of a field, make sure that all the values in the database can be converted to the new type.

So here is the migration file. I'm guessing that the issue is that in_place_lease_term field. That has always been a float just added the default and validator. The site is working fine, just need to find a way through this so that I can do future migrations. Really appreciate it.

import django.core.validators
from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('dashboard', '0008_propertyuniquetoken_token_active'),
    ]

    operations = [
        migrations.AlterField(
            model_name='mla',
            name='leasing_commissions_var',
            field=models.CharField(choices=[('% of Deal', '% of Deal'), ('Amount', 'Amount'), ('Per SF', 'Per SF')], max_length=12),
        ),
        migrations.AlterField(
            model_name='tenant',
            name='in_place_lease_term',
            field=models.FloatField(default=0, validators=[django.core.validators.MinValueValidator(0)]),
        ),
        migrations.AlterField(
            model_name='tenant',
            name='in_place_lease_term_var',
            field=models.CharField(choices=[('Years', 'Years'), ('Months', 'Months')], default='Years', max_length=12),
        ),
    ]

Find the entry in your database that cannot be converted to a float and fix it.

I am not entirely sure how to do that. Sorry in advance it’s it’s too basic of a question.

I guess what’s the best way to access the database and specifically that field?

You can use a MySQL console to connect to and query your database.

I tried using pgAdmin but it says that the server is older and is not supported. I tried doing a query and it won't connect to the server. I am not sure what to do.

You need to upgrade the Postgres server then -- please contact us at support@pythonanywhere.com and we'll discuss the details.

Ok, thank you. I will contact support.

If anyone else is interested I fixed the issue. I have windows and in order to access the SSH tunnel you need to enable it first in your system and then run the command through the command prompt (not SQL prompt like I was doing).

Although the functionality that I had in pgAdmin was limited I was able to macgyver a query that showed me all the entries in the problematic column. Looked through all of them and they all looked like numbers. My best guess is that at least one of them was saved as a string instead of a float.

I removed the last migration file (009 in my case) checked back to see what was the last state of that field before that last migration, removed the change from the model, reran make migrations without that change, and then finally ran the migration. Everything worked. I did it first locally and then on pythonanywhere. The migration went through and everything is fine.