在PostgreSQL中Django是多个unique_together错误映射的吗?

时间:2017-08-23 11:57:28

标签: python django postgresql orm django-1.11

当我在django模型中添加多个unique_together元组时,PostgreSQL数据库映射显示错误(同样根据a previous SO answer on the subject)。下面是一个例子。

这是我的迁移:

migrations.CreateModel(
    name='FinancialConcept',
    fields=[
        ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
        ('taxonomy', models.CharField(max_length=128)),
        ('name', models.CharField(max_length=256)),
        ('xbrl_element', models.CharField(max_length=256)),
        ('parent', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='analysis.FinancialConcept')),
    ],
),
migrations.AlterUniqueTogether(
    name='financialconcept',
    unique_together=set([('taxonomy', 'name'), ('taxonomy', 'xbrl_element'), ('parent', 'xbrl_element')]),
),
....

我预计会创建三个UNIQUE索引,每对一个。但相反,数据库映射的最终结果是:

    Column    |          Type          |                               Modifiers                                
--------------+------------------------+------------------------------    ------------------------------------------
 id           | integer                | not null default nextval('analysis_financialconcept_id_seq'::regclass)
 taxonomy     | character varying(128) | not null
 name         | character varying(256) | not null
 xbrl_element | character varying(256) | not null
 parent_id    | integer                | 
Indexes:
    "analysis_financialconcept_pkey" PRIMARY KEY, btree (id)
    "analysis_financialconcept_taxonomy_xbrl_element_d8b45254_uniq" UNIQUE CONSTRAINT, btree (taxonomy, xbrl_element)
    "analysis_financialconcept_parent_id_51fc8021" btree (parent_id)

即,其中一个UNIQUE索引是正确的,但其他两个索引丢失了。

知道造成这种情况的原因是什么?

1 个答案:

答案 0 :(得分:0)

你尝试过元组元组吗?

unique_together = (('taxonomy', 'name'), ('taxonomy', 'xbrl_element'), ('parent', 'xbrl_element'))