gpt4 book ai didi

python - 在 Django 中运行 syncdb 时出现异常

转载 作者:行者123 更新时间:2023-11-29 13:26:40 25 4
gpt4 key购买 nike

当我在我的项目中运行 python manage.py syncdb 时,出现以下异常。

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.

我觉得这个模型的问题在base_category字段

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

我该如何解决这个问题?

最佳答案

我在我的项目中测试了这段代码,一切正常,但我使用了 manage.py migrate 而不是 manage.py syncdb

我认为问题出在数据库上。
我找到这篇文章,也许可以帮助你解决这个问题。 Link

关于python - 在 Django 中运行 syncdb 时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32683000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com