0

I want to use a state machine in my SQLModel schema but I'm getting type errors.

class Invoice(BaseSQLModel, table=True):
  id: Optional[int] = Field(primary_key=True)
  def __init__(self):
     super().__init__()
     default_status = fsm.initial_payment_state
     self.fsm = fsm.get_payment_fsm(default_status). # Throws an error here because fsm is not an attribute of the class

Is it possible to declare an attribute which is ignored when creating columns? How can I declare the fsm and state attributes here?

I've tried this but then the code gets stuck when trying to call fsm.add_model(self)

class Invoice(BaseSQLModel, table=True):
  id: Optional[int] = Field(primary_key=True)
  _fsm: "Machine" = PrivateAttr()

  def __init__(self):
     super().__init__()
     default_status = fsm.initial_payment_state
     self._fsm = fsm.get_payment_fsm(default_status)
     self._fsm.add_model(self). # Gets stuck here
    

    # Allowing me to declare additional attributes
    class Config:
        extra = Extra.allow
        arbitrary_types_allowed = True
Dave Cook
  • 617
  • 1
  • 5
  • 17
  • 1
    Any property starting with `_` will be ignored by pydantic by default; that might be the case for SQLModel as well, since it uses Pydantic underneath - so `self._fsm` instead? (or you could try defining `fsm` and its type, but not assign it a `Field`?) – MatsLindh Jul 04 '22 at 22:02
  • Thanks @MatsLindh! That's about as far as I've got so far. The issue is that when I call `fsm.add_model(self)` the code gets stuck. I guess because the state machine is trying to add a bunch of other methods and attributes to the class. – Dave Cook Jul 04 '22 at 22:03

0 Answers0