Skip to content
Advertisement

Django index unique on where

I have a class, which represents photos attached to a person.

JavaScript

I want to make sure that for every person there could be only one main photo. In pure SQL i would use something like

JavaScript

You can play with it here http://sqlfiddle.com/#!15/34dfe/4

How to express such constraint in django model? I am using django 4.0.1

Advertisement

Answer

You can add a UniqueConstraint to your model Meta.constraints with a condition to enable this constraint

JavaScript
Advertisement