我的应用程序中有一个名为“就业”的模型,该模型引用了两个名为“用户”和“公司”的模型。在数据库的“就业”表中,两个外键上有一个复合索引,可防止同一用户在同一家公司获得两项工作。我正在尝试使用UniqueConstraint类在Django中表示此复合索引,但是它引发了一个错误,我找不到任何文档。
此模型通常可以正常工作。但是,当我将UniqueConstraint添加到模型的索引时,服务器将引发错误。我看过UniqueConstraint的文档以及Django的其余文档,但是找不到我收到的错误的任何提及。我还尝试在UniqueConstraint的fields参数中将“ User_ID”和“ Company_ID”替换为“ user”和“ company”,但我得到了完全相同的错误。
不确定是否与此问题相关,但是我正在Django版本2.2的python:3.6容器中运行带有Docker的Django服务器。
这是就业模式:
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'
如果有人遇到此错误或知道解决方法,我将非常感谢您的见解。
答案 0 :(得分:0)
您在 参数中定义了此参数,但这应该在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"
constraints = [
models.UniqueConstraint(
fields=['User_ID', 'Company_ID'],
name='Employment_User_Company_UNIQUE'
)
]