gpt4 book ai didi

python - Django: AttributeError: 'UniqueConstraint' 对象没有属性 'fields_orders'

转载 作者:太空宇宙 更新时间:2023-11-04 04:09:56 25 4
gpt4 key购买 nike

我的应用程序中有一个名为“就业”的模型,它引用了两个名为“用户”和“公司”的模型。在数据库的 Employments 表中,两个外键上有一个复合索引,防止同一用户在同一家公司有两个职位。我正在尝试使用 UniqueConstraint 类在 Django 中表示此复合索引,但它抛出了一个错误,我找不到任何文档。

这个模型通常工作得很好。但是,当我将 UniqueConstraint 添加到模型的索引时,服务器会抛出错误。我查看了 UniqueConstraint 的文档,以及 Django 文档的其余部分,但找不到任何关于我收到的错误的提及。我还尝试在 UniqueConstraint 的字段参数中将“User_ID”和“Company_ID”替换为“user”和“company”,但我得到了完全相同的错误。

不确定这是否与问题相关,但我在 python:3.6 容器和 Django 版本 2.2 中运行带有 Docker 的 Django 服务器。

这是 Employment 模型:

from django.db import models
from app.models import User, Company

class Employment(models.Model):
Employment_ID = models.AutoField(primary_key=True)
user = models.ForeignKey(User, db_column='User_ID', on_delete=models.CASCADE)
company = models.ForeignKey(Company, db_column='Company_ID', on_delete=models.CASCADE)

class Meta:
db_table = "Employments"
indexes = [
models.UniqueConstraint(fields=['User_ID', 'Company_ID'],
name='Employment_User_Company_UNIQUE') # server works fine without this
]

当我启动我的 Django 服务器时,我得到以下输出:

Watching for file changes with StatReloader
Performing system checks...

Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/threading.py", line 916, in _bootstrap_inner
self.run()
File "/usr/local/lib/python3.6/threading.py", line 864, in run
self._target(*self._args, **self._kwargs)
File "/usr/local/lib/python3.6/site-packages/django/utils/autoreload.py", line 54, in wrapper
fn(*args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 117, in inner_run
self.check(display_num_errors=True)
File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 390, in check
include_deployment_checks=include_deployment_checks,
File "/usr/local/lib/python3.6/site-packages/django/core/management/base.py", line 377, in _run_checks
return checks.run_checks(**kwargs)
File "/usr/local/lib/python3.6/site-packages/django/core/checks/registry.py", line 72, in run_checks
new_errors = check(app_configs=app_configs)
File "/usr/local/lib/python3.6/site-packages/django/core/checks/model_checks.py", line 31, in check_all_models
errors.extend(model.check(**kwargs))
File "/usr/local/lib/python3.6/site-packages/django/db/models/base.py", line 1254, in check
*cls._check_indexes(),
File "/usr/local/lib/python3.6/site-packages/django/db/models/base.py", line 1565, in _check_indexes
fields = [field for index in cls._meta.indexes for field, _ in index.fields_orders]
File "/usr/local/lib/python3.6/site-packages/django/db/models/base.py", line 1565, in <listcomp>
fields = [field for index in cls._meta.indexes for field, _ in index.fields_orders]
AttributeError: 'UniqueConstraint' object has no attribute 'fields_orders'

如果有人遇到过此错误或知道解决方法,我将非常感谢您的见解。

最佳答案

您在 indexes 参数中定义了它,但这应该在 constraints [Django-doc] 中属性。顾名思义,indexes 包含在表上定义的索引列表。另一方面,constraints 包含应该强制执行的约束。

class Employment(models.Model):
Employment_ID = models.AutoField(primary_key=True)
user = models.ForeignKey(User, db_column='User_ID', on_delete=models.CASCADE)
company = models.ForeignKey(Company, db_column='Company_ID', on_delete=models.CASCADE)

class Meta:
db_table = "Employments"
<b>constraints</b> = [
models.UniqueConstraint(
fields=['User_ID', 'Company_ID'],
name='Employment_User_Company_UNIQUE'
)
]

关于python - Django: AttributeError: 'UniqueConstraint' 对象没有属性 'fields_orders',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56501016/

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