Trouble changing from sqlite3 to postgres

98 views
Skip to first unread message

tony....@gmail.com

unread,
Feb 17, 2015, 3:13:30 PM2/17/15
to django...@googlegroups.com
I have written a simple Django app (my first) that works with sqlite3 database.
I want to change to postgres, but when I run the Django 1.7 migration utility  with the command
"python manage.py migrate"

I get the error:
    psycopg2.ProgrammingError: column "date" cannot be cast automatically to type integer

Which is occuring in:
  File "/usr/local/lib/python3.3/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params

I am not sure how to track down the problem.
I am running Slackware Linux.

My models.py file is as follows:

====================================
from django.db import models
import datetime

# Create your models here.

class Location(models.Model):

   class Meta:
      unique_together = ("lat", "lng")

   lat  = models.DecimalField(max_digits=8, decimal_places=5)
   lng  = models.DecimalField(max_digits=8, decimal_places=5)
   name = models.CharField(max_length=200,  unique=True)

   def __str__(self):
      return "%s: %d %d" % (self.name, self.lat, self.lng)

class Observation(models.Model):

   date     = models.DateField()
   location = models.ForeignKey(Location)
   observer = models.CharField(max_length=50)
   temperature     = models.FloatField(default=0.0)
   photo    = models.ImageField(default="tower.jpg", upload_to="uploaded_photos")

   def __str__(self):
      return self.observer
=========================================

The DATABASE part of my settings.py file is as follows
where I have commented-out the old sqlite3 part:

-----------------------------------------------------------------
DATABASES = {

#   'default': {
#       # sqllite3
#       'ENGINE': 'django.db.backends.sqlite3',
#       'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
#   }

   # For postgres:
    #'django.db.backends.postgresql_psycopg2',
    # USER, PASSWORD, HOST also needed
    # Inside postgres, "CREATE DATABASE database_name"

     'default': {
         'ENGINE': 'django.db.backends.postgresql_psycopg2',
         'NAME': 'app_1_db',
         'USER': 'bill',
         'PASSWORD': 'bill',
         'HOST': '127.0.0.1',
         'PORT': '5432',
     }

}
--------------------------------------------------------------------------------

Please let me know if you have ideas.

Thanks.

Joel Burton

unread,
Feb 17, 2015, 7:39:27 PM2/17/15
to django...@googlegroups.com
The error is probably in code you wrote that uses the date field. Can you post the full traceback? That will let us see where the caller was that created the problem.

tony....@gmail.com

unread,
Feb 17, 2015, 9:22:30 PM2/17/15
to django...@googlegroups.com
The error occurs during migration.
The following shows the complete traceback.
Thanks.
------------------------------------
% python manage migrate
WFK: BASE_DIR= /home/bill/django/wfkprojs/proj1
WFK: STATIC_PATH= /home/bill/django/wfkprojs/proj1/app_1/static/
WFK: MEDIA_ROOT= /home/bill/django/wfkprojs/proj1/app_1/media_root/
WFK: MEDIA_URL= /media_root/
Operations to perform:
  Apply all migrations: contenttypes, app_1, sessions, auth, admin
Running migrations:
  Applying app_1.0003_auto_20141126_2333...Traceback (most recent call last):

  File "/usr/local/lib/python3.3/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)

psycopg2.ProgrammingError: column "date" cannot be cast automatically to type integer
HINT:  Specify a USING expression to perform the conversion.


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.3/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.3/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.3/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/local/lib/python3.3/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.3/site-packages/django/core/management/commands/migrate.py", line 160, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "/usr/local/lib/python3.3/site-packages/django/db/migrations/executor.py", line 63, in migrate
    self.apply_migration(migration, fake=fake)
  File "/usr/local/lib/python3.3/site-packages/django/db/migrations/executor.py", line 97, in apply_migration
    migration.apply(project_state, schema_editor)
  File "/usr/local/lib/python3.3/site-packages/django/db/migrations/migration.py", line 107, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
  File "/usr/local/lib/python3.3/site-packages/django/db/migrations/operations/fields.py", line 139, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/usr/local/lib/python3.3/site-packages/django/db/backends/schema.py", line 473, in alter_field
    self._alter_field(model, old_field, new_field, old_type, new_type, old_db_params, new_db_params, strict)
  File "/usr/local/lib/python3.3/site-packages/django/db/backends/schema.py", line 626, in _alter_field
    params,
  File "/usr/local/lib/python3.3/site-packages/django/db/backends/schema.py", line 99, in execute
    cursor.execute(sql, params)
  File "/usr/local/lib/python3.3/site-packages/django/db/backends/utils.py", line 81, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)

  File "/usr/local/lib/python3.3/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python3.3/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/local/lib/python3.3/site-packages/django/utils/six.py", line 549, in reraise
    raise value.with_traceback(tb)

  File "/usr/local/lib/python3.3/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: column "date" cannot be cast automatically to type integer
HINT:  Specify a USING expression to perform the conversion.

Collin Anderson

unread,
Feb 19, 2015, 1:57:59 PM2/19/15
to django...@googlegroups.com
Hi,

It seems strange that it would be trying to convert a "date" column to an integer.

Since you're creating a new database from scratch, you could try deleting your migrations and generating them from scratch to see if that helps.

Otherwise, what does your "app_1.0003_auto_20141126_2333" file have for operations?

Collin
Reply all
Reply to author
Forward
0 new messages