Django中的'ProgrammingError: 列不存在

0 投票
1 回答
88 浏览
提问于 2025-04-14 18:05

我最近把自己的网站开发转移到了Docker上。我把原来的sqlite数据库换成了postgresql,然后在Docker环境中运行了命令 docker-compose exec web python manage.py migrate。我还更新了设置文件settings.py中的 MEDIA ROOTMEDIA 配置,并且更新了mysite/urls.py。当我去 127.0.0.1:8000/admin 查看存储或上传的文件时,出现了一个错误:

Traceback (most recent call last):
  File "/usr/local/lib/python3.11/site-packages/django/db/backends/utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The above exception (column human_requirementschat.alias does not exist
LINE 1: SELECT "human_requirementschat"."id", "human_requirementscha...
                                              ^
) was the direct cause of the following exception:
  File "/usr/local/lib/python3.11/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/contrib/admin/options.py", line 688, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/utils/decorators.py", line 134, in _wrapper_view
    response = view_func(request, *args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/views/decorators/cache.py", line 62, in _wrapper_view_func
    response = view_func(request, *args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/contrib/admin/sites.py", line 242, in inner
    return view(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/utils/decorators.py", line 46, in _wrapper
    return bound_method(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/utils/decorators.py", line 134, in _wrapper_view
    response = view_func(request, *args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/contrib/admin/options.py", line 2065, in changelist_view
    "selection_note": _("0 of %(cnt)s selected") % {"cnt": len(cl.result_list)},
                                                           ^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/db/models/query.py", line 380, in __len__
    self._fetch_all()
    ^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/db/models/query.py", line 1881, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/db/models/query.py", line 91, in __iter__
    results = compiler.execute_sql(
              
  File "/usr/local/lib/python3.11/site-packages/django/db/models/sql/compiler.py", line 1560, in execute_sql
    cursor.execute(sql, params)
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/db/backends/utils.py", line 102, in execute
    return super().execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(
           
  File "/usr/local/lib/python3.11/site-packages/django/db/backends/utils.py", line 80, in _execute_with_wrappers
    return executor(sql, params, many, context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/db/backends/utils.py", line 84, in _execute
    with self.db.wrap_database_errors:
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/db/utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/django/db/backends/utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Exception Type: ProgrammingError at /admin/human/requirementschat/
Exception Value: column human_requirementschat.alias does not exist
LINE 1: SELECT "human_requirementschat"."id", "human_requirementscha...
                                              ^

以下是相关的文件:

urls.py:

from django.conf import settings # new
from django.conf.urls.static import static # new
from django.contrib import admin
from django.urls import include, path
#from translator import views

urlpatterns = [
    path('', include('homepage.urls')),#redirects to transcribe/,
    path('transcribe/', include('transcribe.urls')),
    path('human/', include('human.urls')),
    path('admin/', admin.site.urls),
]+ static(
settings.MEDIA_URL, document_root=settings.MEDIA_ROOT
) # new

settings.py:

DATABASES = {
"default": env.dj_db_url("DATABASE_URL",
default="postgres://postgres@db/postgres")
}#new

MEDIA_URL='/media/' #new
MEDIA_ROOT = BASE_DIR/'media' #new

models.py:

class RequirementsChat(models.Model):
    id = models.CharField(primary_key=True, max_length=38)
    #id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, max_length=37)
    #id_temp = models.UUIDField(default=uuid.uuid4, unique=True)
    alias = models.CharField(max_length=20, blank=True, null=True)
    email = models.CharField(max_length=80, blank=True, null=True)
    language = models.CharField(max_length=10, blank=True, null=True)
    due_date = models.CharField(max_length=10, blank=True, null=True)
    subtitle_type = models.CharField(max_length=10, blank=True, null=True)
    transcript_file_type = models.CharField(max_length=10, blank=True, null=True)
    additional_requirements = models.TextField(max_length=500, blank=True, null=True)
    date = models.DateTimeField(auto_now_add=True, blank=True, null=True)
    url = models.CharField(max_length=250, blank=True, null=True)
    task_completed = models.BooleanField(default=False)
    
class UploadedFile(models.Model):
    input_file = models.FileField(upload_to='human_upload/')#new
    chat_id = models.CharField(max_length=33, null= True)
    requirements_chat = models.ForeignKey(RequirementsChat, on_delete=models.CASCADE, related_name='human_upload', null=True)

我删除了所有的迁移文件,并多次运行了 makemigrationsmakemigrations humanmigrate,但总是出现同样的错误。非常感谢任何帮助。

1 个回答

1

@tthheemmaannii

首先,请在你的 settings.py 文件中打印出这个变量 DATABASES,看看现在正在使用的数据库是什么。

然后,如果当前数据库没有数据,可以尝试创建一个新的数据库,并用这个新的数据库来创建迁移和进行迁移操作。

撰写回答