Skip to content Skip to sidebar Skip to footer

Column "date" Cannot Be Cast Automatically To Type Timestamp With Time Zone Django/postgres

There were a date charfield , when I saved date as string before. But now I changed the field from charfield to datetimefield and dropped all previous data. date = models.DateTime

Solution 1:

To anyone finding this question on Google when trying to convert an integer column to a timestamp:

ALTERTABLE my_table
ALTERCOLUMN my_column
TYPE timestampwithtime zone
USING to_timestamp(my_column);

Solution 2:

If your column is not fully convertable from the original type from the new one, consider to drop the old column and create a new one:

  • comment out the column field in the model
  • makemigration (will be generated a delete migration for this field) -> YOU WILL DROP ALL COLUMN DATA
  • migrate Leave the comment in the model run again makemigration (now will be generated a new migration with the new column with the new datatype)
  • run again migrate to apply the changes

This works well if you have few fields, but if you have many fields to convert i recommend to write a specific datamigration for that

Solution 3:

Run this in postgres shell :

ALTERTABLE table_name ALTERCOLUMN col_name TYPE timestampwithtime zone USING col_name::timestampwithtime zone

Post a Comment for "Column "date" Cannot Be Cast Automatically To Type Timestamp With Time Zone Django/postgres"