Search code examples
pythondjangopostgresqldjango-syncdb

Getting exception when run syncdb in Django


When I run python manage.py syncdb in my project, I get the following exception.

Traceback (most recent call last):
File "/home/dima/envs/koncertru/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
psycopg2.ProgrammingError: column "category_id" 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 "/opt/pycharm-4.5.4/helpers/pycharm/django_manage.py", line 41, in <module>
run_module(manage_file, None, '__main__', True)


File "/usr/lib/python3.4/runpy.py", line 182, in run_module
    return _run_module_code(code, init_globals, run_name, mod_spec)
  File "/usr/lib/python3.4/runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "/usr/lib/python3.4/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/dima/Python/koncertru/manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/dima/envs/koncertru/lib/python3.4/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/home/dima/envs/koncertru/lib/python3.4/site-packages/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/dima/envs/koncertru/lib/python3.4/site-packages/django/core/management/base.py", line 393, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/dima/envs/koncertru/lib/python3.4/site-packages/django/core/management/base.py", line 444, in execute
    output = self.handle(*args, **options)
  File "/home/dima/envs/koncertru/lib/python3.4/site-packages/django/core/management/commands/syncdb.py", line 25, in handle
    call_command("migrate", **options)
  File "/home/dima/envs/koncertru/lib/python3.4/site-packages/django/core/management/__init__.py", line 120, in call_command
    return command.execute(*args, **defaults)
  File "/home/dima/envs/koncertru/lib/python3.4/site-packages/django/core/management/base.py", line 444, in execute
    output = self.handle(*args, **options)
  File "/home/dima/envs/koncertru/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 222, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "/home/dima/envs/koncertru/lib/python3.4/site-packages/django/db/migrations/executor.py", line 110, in migrate
    self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial)
  File "/home/dima/envs/koncertru/lib/python3.4/site-packages/django/db/migrations/executor.py", line 148, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/dima/envs/koncertru/lib/python3.4/site-packages/django/db/migrations/migration.py", line 115, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/home/dima/envs/koncertru/lib/python3.4/site-packages/django/db/migrations/operations/fields.py", line 201, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/home/dima/envs/koncertru/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 484, in alter_field
    old_db_params, new_db_params, strict)
  File "/home/dima/envs/koncertru/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 636, in _alter_field
    params,
  File "/home/dima/envs/koncertru/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 111, in execute
    cursor.execute(sql, params)
  File "/home/dima/envs/koncertru/lib/python3.4/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/home/dima/envs/koncertru/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/home/dima/envs/koncertru/lib/python3.4/site-packages/django/db/utils.py", line 97, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/dima/envs/koncertru/lib/python3.4/site-packages/django/utils/six.py", line 658, in reraise
    raise value.with_traceback(tb)
  File "/home/dima/envs/koncertru/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: column "category_id" cannot be cast automatically to type integer
HINT:  Specify a USING expression to perform the conversion.

I think the problem in this model in base_category field

class Category(models.Model):
    categories = models.Manager()
    base_category = models.ForeignKey('self', default=None, null=True, blank=True)
    name = models.CharField(max_length=255)
    description = models.TextField()

    class Meta:
        db_table = 'category'
        ordering = ['id']

    def __str__(self):
        return self.name 

How can I fix this?


Solution

  • I Test this code in my project, and all works fine, but i used manage.py migrate instead of manage.py syncdb.

    I think the problem is on database.
    I found this article, maybe can help u, to solve this. Link