2

I'm migrating a mysql database to postgresql in Django and have encountered the following error:

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

The error is referring the this line in my model:

end_date = models.DateField()

The solutions online that I have found have been altering the sql directly, is there a way to correct the model in Django to work with the postgresql database?

Here is the full error, which occurs on migration:

Running migrations:
  Applying catalog.0006_auto_20141121_0419...Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 161, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/migrations/executor.py", line 68, in migrate
    self.apply_migration(migration, fake=fake)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/migrations/executor.py", line 102, in apply_migration
    migration.apply(project_state, schema_editor)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/migrations/migration.py", line 108, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/migrations/operations/fields.py", line 139, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/schema.py", line 455, in alter_field
    self._alter_field(model, old_field, new_field, old_type, new_type, old_db_params, new_db_params, strict)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/schema.py", line 601, in _alter_field
    params,
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/schema.py", line 103, in execute
    cursor.execute(sql, params)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/utils.py", line 81, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: column "end_date" cannot be cast automatically to type timestamp with time zone
HINT:  Specify a USING expression to perform the conversion.
6
  • Did you specify a timezone in settings.py? Commented Jan 16, 2015 at 2:36
  • Adding a timezone didn't seem to have an effect Commented Jan 16, 2015 at 2:55
  • DateField should work work Postgres out-of-the-box. Are you getting this error when trying to run migrations or a script to import your data? Commented Jan 16, 2015 at 2:57
  • I'm getting the error when running migrations Commented Jan 16, 2015 at 3:00
  • 1
    Unfortunately, I'm out of time this evening, but you'll definitely want to post your migration code. Commented Jan 16, 2015 at 3:01

1 Answer 1

1

My work around was to divide it to two migrations.

1 FAKE migrate your migration

2 comment out the field (remove from model)

3 migrate

4 uncomment field(add the field)

5 migrate

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.