Fix “django.db.utils.ProgrammingError: column cannot be cast automatically”

Error below appear when I change “CharField” in my model to “DateTimeField”.

django.db.utils.ProgrammingError: column "created_date" cannot be cast automatically to type timestamp with time zone
HINT:  Specify a USING expression to perform the conversion.

And error below when I change “CharField” in my model to “SmallIntegerField”.

django.db.utils.ProgrammingError: column "status_type" cannot be cast automatically to type smallint
HINT:  Specify a USING expression to perform the conversion.

I solved those error by commenting on field that I need to change and make new migration, so those field will deleted in my database.
After that I uncomment those fields and make migration with new field type.

If you found better solution, let me know and don’t hesitate to comment. 😀

Advertisements

One comment

  1. In the failing migration file, edit the class Migration and under dependencies, add a new dependency at the end of the list. Create this file, such as 0002_modelname_manual_remove_bad_columns.

    In the new 0002_modelname_manual_remove_bad_columns.py file, create a class Migration with dependency on the previous migration. The operations property should have a series of migrations.RemoveField() calls of the bad fields. You can copy and paste and string replace AddField (or AlterField) and just remove the field keyword argument.

    In the failing migration file, change all the AlterField() calls into AddField() (simple string replace).

    Commit these changes and apply these two migrations without fail 🙂

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s