Hi everyone,
I’m trying to add contraints to a model with multiple many to many fields.
Basically i would like to ensure that at least one value is entered for tenant_person OR for tenant_company. An d the same for the landord_tenant and landolord_company
I did write this constraints:
class Meta:
constraints = [
models.CheckConstraint(
name="%(app_label)s_%(class)s_tenant_not_null",
check=(models.Q(tenant_person__isnull=False) | models.Q(tenant_company__isnull=False))
),
models.CheckConstraint(
name="%(app_label)s_%(class)s_landlord_not_null",
check=(models.Q(landlord_person__isnull=False) | models.Q(landlord_company__isnull=False))
)
]
When I make the migrations everything is fine but when I migrate I get this error:
django.db.utils.ProgrammingError: column “person_id” does not exist
My DB backend is Postgres in a docker
I looked around in the doc, stackoverflow and the forum… I guess that it might be related to the type (ManyToMany) of the fields.
Any help, direction to the right documentation would be much appreciated
Axel
Here is my full model:
class RentContract(models.Model):
#
# BANK_TRANSFER = 'bank transfer'
# BLOCKED_ACCOUNT = 'blocked account'
# CASH = 'cash'
# DEPOSIT_TYPE = [
# (BANK_TRANSFER, 'Bank transfer'),
# (BLOCKED_ACCOUNT, 'Blocked account'),
# (CASH, 'Cash'),
# ]
property = models.ForeignKey('real_estate.Property', on_delete=models.PROTECT)
tenant_person = models.ManyToManyField('address_book.Person', related_name='tenant_person', default=None,
blank=True)
tenant_company = models.ManyToManyField('address_book.Company', related_name='tenant_company', default=None,
blank=True)
tenant_representative = models.ForeignKey('address_book.Person', on_delete=models.PROTECT,
related_name='tenant_representative', default=None, null=True, blank=True)
landlord_person = models.ManyToManyField('address_book.Person', related_name='landlord_person', default=None,
blank=True)
landlord_company = models.ManyToManyField('address_book.Company', related_name='landlord_company', default=None,
blank=True)
landlord_representative = models.ForeignKey('address_book.Person', on_delete=models.PROTECT,
related_name='landlord_representative', default=None, null=True,
blank=True)
date_signed = models.DateField()
date_start = models.DateField()
length = models.IntegerField(default=None, blank=True, null=True)
date_stop = models.DateField(default=None, blank=True, null=True)
renew_auto = models.BooleanField()
stop_normal_notification = models.IntegerField(default=2)
stop_normal_indemnity = models.FloatField(default=2)
stop_pro_notification = models.IntegerField(default=1)
stop_pro_indemnity = models.FloatField(default=1)
rent = models.FloatField()
expanses = models.FloatField()
payment_target_account = models.ForeignKey('accounting.BankAccount', on_delete=models.PROTECT)
# deposit = models.FloatField()
# deposit_type = models.CharField(max_length=25, choices=DEPOSIT_TYPE, default=BLOCKED_ACCOUNT)
timestamp_updated = models.DateTimeField(auto_now=True)
timestamp_created = models.DateField(auto_now_add=True)
comment = models.TextField(default=None, blank=True, null=True)
note = models.TextField(default=None, blank=True, null=True)
def __str__(self):
return f"{self.property.name} - {self.date_start}"
class Meta:
constraints = [
models.CheckConstraint(
name="%(app_label)s_%(class)s_tenant_not_null",
check=(models.Q(tenant_person__isnull=False) | models.Q(tenant_company__isnull=False))
),
models.CheckConstraint(
name="%(app_label)s_%(class)s_landlord_not_null",
check=(models.Q(landlord_person__isnull=False) | models.Q(landlord_company__isnull=False))
)
]