1

When I add multiple unique_together tuples in my django model, the PostgreSQL DB mapping appears wrong (also according to a previous SO answer on the subject). An example follows.

This is my migration:

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')]),
),
....

which I expected would create three UNIQUE indexes, one for each pair. But instead, what the DB mapping ends up being is this:

    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)

I.e, one of the UNIQUE indexes is right, but the other two are missing.

Any idea what is causing this?

clapas
  • 1,768
  • 3
  • 16
  • 29

1 Answers1

0

Did you try tuple of tuples?

unique_together = (('taxonomy', 'name'), ('taxonomy', 'xbrl_element'), ('parent', 'xbrl_element'))
Łukasz Kamiński
  • 5,630
  • 1
  • 19
  • 32
  • Just tried but it makes no difference. The bracket is (as is the whole migration) actually generated by django's `makemigration` command. – clapas Aug 23 '17 at 17:08