1

I'm trying to migrate my Django project from using sqlite3 to using PostgreSQL. I've created the database for the project, but when I try to run syncdb, I get the following error:

django.db.utils.ProgrammingError: column "partial_value" cannot be cast automatically to type double precision
HINT: Specify a USING expression to perform the conversion.

The column is defined like this in the model:

partial_value = models.FloatField()

I tried searching for similar questions, but they seemed to be more about replacing field types.

manabreak
  • 5,415
  • 7
  • 39
  • 96

2 Answers2

1

You needs to update the type of column for 'partial_value' column. Place name of table in following code for table_name

ALTER TABLE table_name ALTER COLUMN partial_value TYPE double precision USING (trim(partial_value)::double precision);
navyad
  • 3,752
  • 7
  • 47
  • 88
  • Could you explain why the error happens in the first place? I checked the type of the column and it was of type 'character varying' instead of double precision. – manabreak May 14 '15 at 15:28
  • As you said the earlier column type was character , but now it seems it is float type, so the data type of the partial_value is changed by dajngo, But that change is not reflected into the database, that's why error is coming. – navyad May 14 '15 at 15:32
  • No, the column type has always been FloatField in Django. The type in PostgreSQL database was 'character varying'. This is the thing I'm wondering: why does the FloatField convert to 'character varying' ? – manabreak May 14 '15 at 15:45
0

I found the reason to why the field was applied as character varying in the first place. It turns out there was a migration file that defined it that way. The error was fixed by removing the migration file and running manage.py syncdb.

manabreak
  • 5,415
  • 7
  • 39
  • 96
  • see also : https://stackoverflow.com/questions/30948392/django-1-8-migration-unable-to-cast-column-id-to-integer if you need a less specific answer. – jmny Dec 19 '17 at 14:34