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

后端 未结 2 665
我在风中等你
我在风中等你 2021-02-15 02:57

I\'m running Django 1.11 with Python 3.4 on Ubuntu 14.04.5

Moving my development code to the test server and running into some strange errors. Can anyone see what is wr

相关标签:
2条回答
  • 2021-02-15 03:19

    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
    
    
    0 讨论(0)
  • 2021-02-15 03:42

    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);'),
        ]
    
    0 讨论(0)
提交回复
热议问题