psycopg2.DataError: invalid input syntax for integer: "test" Getting error when moving code to test server

The issue is likely related to this open bug in Django. You have some test data in one of the fields that you are now converting to a ForeignKey.

For instance, maybe department used to be a CharField and you added an employee who has "test" as their department value. Now you're trying to change department from a CharField to a ForeignKey. The issue is that Django is trying to convert the previous value "test" into a relational value (integer) for the ForeignKey.

I can think of a few good solutions:

  • If this is just a test database, just reset your database and run the migration on a clean database
  • If you need to migrate the existing data, figure out what field has the "test" value. Then try something similar to the solution given in the bug report:

```

from __future__ import unicode_literals

from django.db import migrations

class Migration(migrations.Migration):
    dependencies = [
        ('documents', '0042_auto_19700101-0000'),
    ]

    operations = [
        migrations.RunSQL('ALTER TABLE documents_document_tags ALTER tag_id TYPE varchar(32);'),
    ]

In my case, I have the same issue on the development. This command works for me.

python manage.py flush

Make sure it removes all data from the database. Run this command, it will delete all data from the database and run migration again.

python manage.py migrate

Tags:

Python

Django