Search code examples
djangopostgresqldjango-modelsdjango-southdjango-1.4

Django 1.4 TimeField migration fails on PostgreSQL


I edited two fields on a model and changed them from IntegerFields to TimeFields:

class Model(models.Model):
    start_time = models.TimeField()
    end_time = models.TimeField()

I'm using these two fields to save a naive time, which is not related to any geographical notion of time, and thus has no real 'time zone' (think something similar to race times). My local database is PostgreSQL.

However, the south migration generated from this change fails with the following error:

> main:0005_auto__chg_field_model_start_time__chg_field_model_end_time
FATAL ERROR - The following SQL query failed: ALTER TABLE "main_model" ALTER COLUMN "start_time" TYPE time, ALTER COLUMN "start_time" SET NOT NULL, ALTER COLUMN "start_time" DROP DEFAULT;

...

File ".../lib/python2.7/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 52, in execute
    return self.cursor.execute(query, args)
django.db.utils.DatabaseError: column "start_time" cannot be cast to type time without time zone

The failed migration has this:

class Migration(SchemaMigration):

    def forwards(self, orm):

        # Changing field 'Model.start_time'
        db.alter_column('main_model', 'start_time', self.gf('django.db.models.fields.TimeField')())

        # Changing field 'Model.end_time'
        db.alter_column('main_model', 'end_time', self.gf('django.db.models.fields.TimeField')())

Any idea on how to make postgres happy about this migration?

P.S. I'm in the midst of development, so I really don't care about any data migrations. You may assume the DB is empty.


Solution

  • Since you don't care about data, the simplest way would be to delete the column then add it again with type Time.

    Either edit the current migration manually to do this. Or delete this migration, then comment the field and run schemamigration --auto then add the field and run it again.