自定义unique_together键名称

时间:2014-12-29 21:23:09

标签: python mysql django django-models unique-constraint

我有一个模型,其中为{3}}定义的3个字段是唯一的:

class MyModel(models.Model):
    clid = models.AutoField(primary_key=True, db_column='CLID')
    csid = models.IntegerField(db_column='CSID')
    cid = models.IntegerField(db_column='CID')
    uuid = models.CharField(max_length=96, db_column='UUID', blank=True)

    class Meta(models.Meta):
        unique_together = [
            ["csid", "cid", "uuid"],
        ]

现在,如果我尝试使用现有的csid + cid + uuid组合保存MyModel实例,我会得到:

IntegrityError: (1062, "Duplicate entry '1-1-1' for key 'CSID'")

哪个是对的。但是,有没有办法自定义该密钥名称? (在这种情况下为CSID

换句话说,我可以为unique_together中列出的约束提供名称吗?

据我了解,文档中未对此进行介绍。

5 个答案:

答案 0 :(得分:7)

它没有详细记录,但取决于你是否使用Django 1.6或1.7,有两种方法可以做到这一点:

In Django 1.6 you can override the unique_error_message, like so

class MyModel(models.Model):
    clid = models.AutoField(primary_key=True, db_column='CLID')
    csid = models.IntegerField(db_column='CSID')
    cid = models.IntegerField(db_column='CID')

    # ....

def unique_error_message(self, model_class, unique_check):
    if model_class == type(self) and unique_check == ("csid", "cid", "uuid"):
        return _('Your custom error')
    else:
        return super(MyModel, self).unique_error_message(model_class, unique_check)

Or in Django 1.7

class MyModel(models.Model):
    clid = models.AutoField(primary_key=True, db_column='CLID')
    csid = models.IntegerField(db_column='CSID')
    cid = models.IntegerField(db_column='CID')
    uuid = models.CharField(max_length=96, db_column='UUID', blank=True)

    class Meta(models.Meta):
        unique_together = [
            ["csid", "cid", "uuid"],
        ]
        error_messages = {
            NON_FIELD_ERRORS: {
                'unique_together': "%(model_name)s's %(field_labels)s are not unique.",
            }
        }

答案 1 :(得分:5)

完整性错误来自数据库但来自django:

create table t ( a int, b int , c int);
alter table t add constraint u unique ( a,b,c);   <-- 'u'    
insert into t values ( 1,2,3);
insert into t values ( 1,2,3);

Duplicate entry '1-2-3' for key 'u'   <---- 'u'

这意味着您需要在数据库中创建具有所需名称的约束。但是django在迁移中命名为约束。查看_create_unique_sql:

def _create_unique_sql(self, model, columns):
    return self.sql_create_unique % {
        "table": self.quote_name(model._meta.db_table),
        "name": self.quote_name(self._create_index_name(model, columns, suffix="_uniq")),
        "columns": ", ".join(self.quote_name(column) for column in columns),
    }

_create_index_name是否具有命名约束的算法:

def _create_index_name(self, model, column_names, suffix=""):
    """
    Generates a unique name for an index/unique constraint.
    """
    # If there is just one column in the index, use a default algorithm from Django
    if len(column_names) == 1 and not suffix:
        return truncate_name(
            '%s_%s' % (model._meta.db_table, self._digest(column_names[0])),
            self.connection.ops.max_name_length()
        )
    # Else generate the name for the index using a different algorithm
    table_name = model._meta.db_table.replace('"', '').replace('.', '_')
    index_unique_name = '_%x' % abs(hash((table_name, ','.join(column_names))))
    max_length = self.connection.ops.max_name_length() or 200
    # If the index name is too long, truncate it
    index_name = ('%s_%s%s%s' % (
        table_name, column_names[0], index_unique_name, suffix,
    )).replace('"', '').replace('.', '_')
    if len(index_name) > max_length:
        part = ('_%s%s%s' % (column_names[0], index_unique_name, suffix))
        index_name = '%s%s' % (table_name[:(max_length - len(part))], part)
    # It shouldn't start with an underscore (Oracle hates this)
    if index_name[0] == "_":
        index_name = index_name[1:]
    # If it's STILL too long, just hash it down
    if len(index_name) > max_length:
        index_name = hashlib.md5(force_bytes(index_name)).hexdigest()[:max_length]
    # It can't start with a number on Oracle, so prepend D if we need to
    if index_name[0].isdigit():
        index_name = "D%s" % index_name[:-1]
    return index_name

对于当前的django版本(1.7),复合唯一约束的约束名称如下所示:

>>> _create_index_name( 'people', [ 'c1', 'c2', 'c3'], '_uniq' )
'myapp_people_c1_d22a1efbe4793fd_uniq'

您应该以某种方式覆盖_create_index_name以更改算法。或许,编写自己的数据库后端,继承自mysql并覆盖_create_index_name schema.py上的DatabaseSchemaEditor(未经测试)

答案 2 :(得分:5)

更改./manage.py sqlall输出中的索引名称。

您可以自己运行./manage.py sqlall并自行添加约束名称并手动应用而不是syncdb

$ ./manage.py sqlall test
BEGIN;
CREATE TABLE `test_mymodel` (
    `CLID` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `CSID` integer NOT NULL,
    `CID` integer NOT NULL,
    `UUID` varchar(96) NOT NULL,
    UNIQUE (`CSID`, `CID`, `UUID`)
)
;

COMMIT;

e.g。

$ ./manage.py sqlall test
BEGIN;
CREATE TABLE `test_mymodel` (
    `CLID` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `CSID` integer NOT NULL,
    `CID` integer NOT NULL,
    `UUID` varchar(96) NOT NULL,
    UNIQUE constraint_name (`CSID`, `CID`, `UUID`)
)
;

COMMIT;

覆盖BaseDatabaseSchemaEditor._create_index_name

@danihp指出的解决方案不完整,只适用于字段更新(BaseDatabaseSchemaEditor._alter_field

我通过覆盖_create_index_name获得的sql是:

BEGIN;
CREATE TABLE "testapp_mymodel" (
    "CLID" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
    "CSID" integer NOT NULL,
    "CID" integer NOT NULL,
    "UUID" varchar(96) NOT NULL,
    UNIQUE ("CSID", "CID", "UUID")
)
;

COMMIT;

覆盖BaseDatabaseSchemaEditor.create_model

基于https://github.com/django/django/blob/master/django/db/backends/schema.py

class BaseDatabaseSchemaEditor(object):
    # Overrideable SQL templates
    sql_create_table_unique = "UNIQUE (%(columns)s)"
    sql_create_unique = "ALTER TABLE %(table)s ADD CONSTRAINT %(name)s UNIQUE (%(columns)s)"
    sql_delete_unique = "ALTER TABLE %(table)s DROP CONSTRAINT %(name)s"

这是create_model感兴趣的部分:

    # Add any unique_togethers
    for fields in model._meta.unique_together:
        columns = [model._meta.get_field_by_name(field)[0].column for field in fields]
        column_sqls.append(self.sql_create_table_unique % {
            "columns": ", ".join(self.quote_name(column) for column in columns),
        })

<强>结论

你可以:

  • 覆盖create_model_create_index_name使用unique_together约束。
  • 修改sql_create_table_unique模板以包含name参数。

您也可以检查此票证的可能修复:

https://code.djangoproject.com/ticket/24102

答案 3 :(得分:2)

我相信你必须在你的数据库中这样做;

MySQL:

ALTER TABLE `votes` ADD UNIQUE `unique_index`(`user`, `email`, `address`);

我相信会说...关键'unique_index'

答案 4 :(得分:2)

一种解决方案是您可以在save()中捕获IntegrityError,然后根据需要制作自定义错误消息,如下所示。

try:
    obj = MyModel()
    obj.csid=1
    obj.cid=1
    obj.uuid=1
    obj.save()

except IntegrityError:
    message = "IntegrityError: Duplicate entry '1-1-1' for key 'CSID', 'cid', 'uuid' "

现在您可以使用此消息显示为错误消息。