django changing a date field to integer field cant migrate

2024/4/15 2:22:06

I recently changed a date field to an integer field (the data was specified in number of months remaining rather than a date).

However all though the make migrations command works fine when I attempt to migrate this fails with a typecast error (note using postgres 9.5). Note there was only 1 instance/entry of the model, which I have also now deleted and the migrate still fails for some reason?

File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/backends/", line 64, in executereturn self.cursor.execute(sql, params)
psycopg2.ProgrammingError: cannot cast type date to integer
LINE 1: ...end_date_fixed" TYPE integer USING "end_date_fixed"::integer^The above exception was the direct cause of the following exception:Traceback (most recent call last):File "", line 10, in <module>execute_from_command_line(sys.argv)File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/core/management/", line 353, in execute_from_command_lineutility.execute()File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/core/management/", line 345, in executeself.fetch_command(subcommand).run_from_argv(self.argv)File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/core/management/", line 348, in run_from_argvself.execute(*args, **cmd_options)File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/core/management/", line 399, in executeoutput = self.handle(*args, **options)File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/core/management/commands/", line 200, in handleexecutor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/migrations/", line 92, in migrateself._migrate_all_forwards(plan, full_plan, fake=fake, fake_initial=fake_initial)File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/migrations/", line 121, in _migrate_all_forwardsstate = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/migrations/", line 198, in apply_migrationstate = migration.apply(state, schema_editor)File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/migrations/", line 123, in applyoperation.database_forwards(self.app_label, schema_editor, old_state, project_state)File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/migrations/operations/", line 201, in database_forwardsschema_editor.alter_field(from_model, from_field, to_field)File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/backends/base/", line 482, in alter_fieldold_db_params, new_db_params, strict)File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/backends/postgresql/", line 110, in _alter_fieldnew_db_params, strict,File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/backends/base/", line 634, in _alter_fieldparams,File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/backends/base/", line 110, in executecursor.execute(sql, params)File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/backends/", line 79, in executereturn super(CursorDebugWrapper, self).execute(sql, params)File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/backends/", line 64, in executereturn self.cursor.execute(sql, params)File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/", line 95, in __exit__six.reraise(dj_exc_type, dj_exc_value, traceback)File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/utils/", line 685, in reraiseraise value.with_traceback(tb)File "/Users/Yunti/.virtualenvs/switcher5/lib/python3.5/site-packages/django/db/backends/", line 64, in executereturn self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: cannot cast type date to integer
LINE 1: ...end_date_fixed" TYPE integer USING "end_date_fixed"::integer

If the table is empty.

Do the migration in two simple steps. Step 1, drop the existing date column from the model. Step 2 add a new INTEGER column with the same column name.

If the table is not empty.

Edit your model to change the type of end_date_fixed to integer as you have done. Do the make migrations, the open the migrations and delete everything inside migrations[] then replacce it with

 migrations.RunSQL("ALTER TABLE mytable ALTER end_date_fixed TYPE INTEGER USING EXTRACT(epoch FROM end_date_fixed)

Make sure that you have not made any other changes to the model when you try this method.

Related Q&A

Sqlalchemy get row in timeslot

I have a model called Appointment which has the columns datetime which is a DateTime field and duration which is an Integer field and represents duration in minutes. Now I want to check if i…

How do I include files in PyPI?

I am a newb to let me qualify with that. I am trying to put a package on PyPI but having a bit of trouble when I try to install it with pip. When I upload the file to PyPI, I get a warning (b…

How to create a custom AutoField primary_key entry within Django

I am trying to create a custom primary_key within my helpdesk/ that I will use to track our help desk tickets. I am in the process of writing a small ticking system for our office.Maybe there …

Multiple HoverTools for different lines (bokeh)

I have more than one line on a bokeh plot, and I want the HoverTool to show the value for each line, but using the method from a previous stackoverflow answer isnt working:…

Cant Install PIL 1.7

I have python 2.7.3 and I want to install PIL 1.7. I downloaded "PIL-1.1.7.win32-py2.7" and try to install it but it shows me an error messege that it cant find python 2.7 in the registry.&qu…

slice/split a layer in keras as in caffe

I have used this converter to convert a Caffe model to Keras. But one of my layers is of type slice and it needs to be converted as well but the converter currently does not support this and raises an …

Can I use regexes within datetime.strptime formats?

I have string values that contain a trailing time stamp. I thought I could use strptime with a regex pattern to extract those. Like:from __future__ import print_functionfrom datetime import datetime # …

Sort a list with longest items first

I am using a lambda to modify the behaviour of sort.sorted(list, key=lambda item:(item.lower(),len(item)))Sorting a list containing the elements A1,A2,A3,A,B1,B2,B3,B, the result is A,A1,A2,A3,B,B1,B2,…

Multivariate Root Finding in Python

Using excel solver, it is easy to find a solution (optimum value for x and y )for this equation: (x*14.80461) + (y * -4.9233) + (10*0.4803) ≈ 0However, I cant figure out how to do this in Python. The …

Python print not working when embedded into MPI program

I have an Python 3 interpreter embedded into an C++ MPI application. This application loads a script and passes it to the interpreter.When I execute the program on 1 process without the MPI launcher (s…