0

I would like to add database constraints to my model, that require at least one of its fields to be not-null. When checking the m2m field, I get a FieldError: Cannot resolve keyword '' into field.

Is it possible to create such constraints?

Sample code:

class A(Model):
    id = AutoField()
    url = ManyToManyField(Url, blank=True)
    description = TextField(null=True, blank=True)

    class Meta:
        constraints = [CheckConstraints(
            check=(Q(description__isnull=False) | Q(url__isnull=False))),
            name="someName"
        )]
AsiPanda
  • 31
  • 7

1 Answers1

0

It is not possible to achieve this using the CheckConstraint functionality. Django translates all ORM commands to the low level DB specific commands, and such constraint creation isn't possible even on the DB level. In fact, we can apply CheckConstraint to a single row beeing added/updated only.

See full answer here: https://stackoverflow.com/a/60799459/15090285