当我在django模型中添加多个unique_together
元组时,PostgreSQL DB映射出现错误(也是根据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
索引,每对一个。但是,DB映射的结果是: 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
索引是正确的,但另两个索引丢失了。知道是什么原因吗?
最佳答案
你试过一对一的吗?
unique_together = (('taxonomy', 'name'), ('taxonomy', 'xbrl_element'), ('parent', 'xbrl_element'))
关于python - Django是否在PostgreSQL中错误地映射了多个unique_together?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45839083/