[Solved]-Psycopg2.DataError: invalid input syntax for integer: "test" Getting error when moving code to test server

25πŸ‘

βœ…

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);'),
    ]
πŸ‘€YPCrumble

2πŸ‘

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

1πŸ‘

In my case error was thith replace IntegerField to `TextField in file migartion

file migration:

# Generated by Django 3.1.3 on 2020-12-03 11:19

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('adminPanel', '0028_auto_20201203_1117'),
    ]

    operations = [
        migrations.RemoveField(
            model_name='myuser',
            name='balance',
        ),
        migrations.AlterField(
            model_name='myuser',
            name='bot_admin_chat_id',
            field=models.IntegerField(default=0),
        ),
    ]

I change IntegerField to TextField & python manage.py migrate

After that, I remove my column in models, makemigration & migrate
After that, I do thth my model that ever I want.

πŸ‘€user348484

0πŸ‘

The simplest way that works for me is changing the foreign key to a character field, Make migrations, migrate. Then change back the field to be a foreign key. This way, you will force a database alteration which is very important

πŸ‘€de Isaac

0πŸ‘

In my case @YPCrumble was correct and it was caused after I changed a field from a StreamField to a CharField.

The quickest solution I found was to open up my DB on a GUI (Postico in my case) and overwrite the values from JSON to the default value. This meant setting every existing fields values to 1, in my situation.

This let the migration run as intended, without issue.

πŸ‘€Natch

0πŸ‘

It has to do with your models file.

You made use of Charfield where there was Foreignkey expectation.

You can simply flush the database to empty it, then migrate again, it will work.

python manage.py flush

Then

python manage.py migrate

πŸ‘€Ukeje Nonso

Leave a comment