Django 1.4 Continuous TimeField Migration to PostgreSQL

I edited two fields on the model and changed them from IntegerField to TimeField s:

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

I use these two fields to save a naive time that is not associated with any geographical concept of time, and therefore does not have a real โ€œtime zoneโ€ (think of something like this race). My local database is PostgreSQL.

However, southern 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 

Failed to migrate:

 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 in this migration?

PS I'm in the midst of development, so I really don't need data migrations. You can assume that the database is empty.

+4
source share
3 answers

Since you do not need data, the easiest way would be to delete the column and then add it again with the type Time .

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

+5
source

I manually edited the carry script (Thanks @Maccesch) that all alter_column() calls are replaced with delete_column() and then add_column() .

Please note that this means that the data transfer is not performed , and any data that existed before this transfer will be deleted.

the code:

 def forwards(self, orm): # NOTE: NO MIGRATION HERE!! # THIS _WILL_ CAUSE DATA LOSS # Changing field 'Model.start_time' db.delete_column('main_model', 'start_time') db.add_column('main_model', 'start_time', self.gf('django.db.models.fields.TimeField')()) # Changing field 'Model.end_time' db.delete_column('main_model', 'end_time') db.add_column('main_model', 'end_time', self.gf('django.db.models.fields.TimeField')()) 

backwards() similarly implemented.

0
source

I also came across this problem, the problem was in the southern migrations when creating a new test database. Accommodation

 SOUTH_TESTS_MIGRATE = False 

The settings file fixed the problem for me.

0
source

Source: https://habr.com/ru/post/1416464/


All Articles