When I execute migrate command, I get the error “reports.CostCenterInYearEncumbrance: (models.E012) ‘constraints’ refers to the nonexistent field ‘period’.”
period does not exist in CostCenterInYearEncumbrance but it does exists in CostCenterMonthlyEncumbrance which inherit from CostCenterInYearEncumbrance as intended. And I cannot see why it is trying to create a constraint for period in CostCenterInYearEncumbrance. Has to be a stupid thing.
Relevant code:
class InYearData(models.Model):
"""A generic base class that contains generic fields to be used by other classes that needs in-year related fields."""
fund = models.CharField("Fund", max_length=4)
source = models.CharField("Source", max_length=24)
costcenter = models.CharField("Cost Center", max_length=6)
fy = models.CharField("FY", max_length=4)
class Meta:
abstract = True
constraints = [
models.UniqueConstraint(
fields=("fund", "source", "costcenter", "fy"),
name="%(app_label)s_%(class)s_is_unique",
)
]
class MonthlyData(InYearData):
"""A generic base class that contains generic fields to be used by other classes that needs monthly related fields."""
# fund = models.CharField("Fund", max_length=4)
# source = models.CharField("Source", max_length=24)
# costcenter = models.CharField("Cost Center", max_length=6)
period = models.CharField("Period", max_length=2)
# fy = models.CharField("FY", max_length=4)
class Meta:
abstract = True
constraints = [
models.UniqueConstraint(
fields=(
"fund",
"source",
"costcenter",
"period",
"fy",
),
name="%(app_label)s_%(class)s_is_unique",
)
]
class CostCenterMonthlyEncumbrance(MonthlyData):
spent = models.DecimalField("Spent", max_digits=10, decimal_places=2, default=0, null=True)
commitment = models.DecimalField("Commitment", max_digits=10, decimal_places=2, default=0, null=True)
pre_commitment = models.DecimalField("Pre Commitment", max_digits=10, decimal_places=2, default=0, null=True)
fund_reservation = models.DecimalField("Fund Reservation", max_digits=10, decimal_places=2, default=0, null=True)
balance = models.DecimalField("Balance", max_digits=10, decimal_places=2, default=0, null=True)
working_plan = models.DecimalField("Working Plan", max_digits=10, decimal_places=2, default=0, null=True)
class Meta(MonthlyData.Meta):
verbose_name_plural = "Cost Center Monthly Encumbrance"
class CostCenterInYearEncumbrance(InYearData):
spent = models.DecimalField("Spent", max_digits=10, decimal_places=2, default=0, null=True)
commitment = models.DecimalField("Commitment", max_digits=10, decimal_places=2, default=0, null=True)
pre_commitment = models.DecimalField("Pre Commitment", max_digits=10, decimal_places=2, default=0, null=True)
fund_reservation = models.DecimalField("Fund Reservation", max_digits=10, decimal_places=2, default=0, null=True)
balance = models.DecimalField("Balance", max_digits=10, decimal_places=2, default=0, null=True)
working_plan = models.DecimalField("Working Plan", max_digits=10, decimal_places=2, default=0, null=True)
class Meta(MonthlyData.Meta):
verbose_name_plural = "Cost Center In Year Encumbrance"
In the migration file, I find the add constraint shows the field period:
migrations.AddConstraint(
model_name="costcenterinyearencumbrance",
constraint=models.UniqueConstraint(
fields=("fund", "source", "costcenter", "period", "fy"),
name="reports_costcenterinyearencumbrance_is_unique",
),
),