constraints refers to the non existing field 'period'

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",
      ),
  ),

IT’s probably because you have added the field and later on removed it, before running the migration?
You can just remove the field “period” from that constraint to try it out and see if it works.

I actually went a little further than that. I deleted all migration files and the db. Yet I still need to remove the field “period” from MonthlyData model for the migrate to work. As soon as I put it back, I end up with the same error message: reports.CostCenterInYearEncumbrance: (models.E012) ‘constraints’ refers to the nonexistent field ‘period’.

I don’t understand because MonthlyData and CostCenterInYearEncumbrance are not even related.

I cloned my project without CostCenterInYearEncumbrance and without InYearData. Put them back in and oddly enough, it seems to be ok now. @leandrodesouzadev , thanks for the pointer anyway. I remained puzzled though.