# How to define two fields “unique” as couple

## How to define two fields “unique” as couple

```Is there a way to define a couple of fields as unique in Django?
I have a table of volumes (of journals) and I don't want more then one volume number for the same journal.
class Volume(models.Model):
id = models.AutoField(primary_key=True)
journal_id = models.ForeignKey(Journals, db_column='jid', null=True, verbose_name = "Journal")
volume_number = models.CharField('Volume Number', max_length=100)

I tried to put unique = True as attribute in the fields journal_id and volume_number but it doesn't work.
```

There is a simple solution for you called unique_together which does exactly what you want.

For example:

``````class MyModel(models.Model):
field1 = models.CharField(max_length=50)
field2 = models.CharField(max_length=50)

class Meta:
unique_together = ('field1', 'field2',)
``````

``````class Volume(models.Model):
id = models.AutoField(primary_key=True)
journal_id = models.ForeignKey(Journals, db_column='jid', null=True, verbose_name = "Journal")
volume_number = models.CharField('Volume Number', max_length=100)

class Meta:
unique_together = ('journal_id', 'volume_number',)
``````

## Django 2.2+

Using the `constraints` features `UniqueConstraint` is preferred over unique_together.

From the Django documentation for `unique_together`:

Use UniqueConstraint with the constraints option instead.
UniqueConstraint provides more functionality than unique_together.
unique_together may be deprecated in the future.

For example:

``````class Volume(models.Model):
id = models.AutoField(primary_key=True)
journal_id = models.ForeignKey(Journals, db_column='jid', null=True, verbose_name="Journal")
volume_number = models.CharField('Volume Number', max_length=100)
{"ft_lang_label":"__label__en","ft_lang_prob":0.5873635,"math_prob":0.82410187,"size":2208,"snap":"2020-10-2020-16","text_gpt3_token_len":488,"char_repetition_ratio":0.15744102,"word_repetition_ratio":0.21862349,"special_character_ratio":0.23958333,"punctuation_ratio":0.18965517,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99649006,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-02-23T23:23:19Z\",\"WARC-Record-ID\":\"<urn:uuid:70ba6116-b75a-4c58-a5ea-789432d6c279>\",\"Content-Length\":\"21857\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:cd439574-941d-4244-b7a1-23e8378031b7>\",\"WARC-Concurrent-To\":\"<urn:uuid:2e70382a-c33b-43c5-b3bf-bebb4bf45bb3>\",\"WARC-IP-Address\":\"107.191.41.26\",\"WARC-Target-URI\":\"https://loitools.com/blog/how-to-define-two-fields-unique-as-couple-2/\",\"WARC-Payload-Digest\":\"sha1:A3YAGC42MMBL5HFGR76AIMU4BDGLFPVU\",\"WARC-Block-Digest\":\"sha1:PIY6ANEDD3YTSVCEPH6GKTSTAPSMQPZH\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-10/CC-MAIN-2020-10_segments_1581875145859.65_warc_CC-MAIN-20200223215635-20200224005635-00379.warc.gz\"}"}