title: Flask-SQLALchemy update record automatically after specific time
body: <p>I have a db models like this:</p>
  
  <pre><code>class Payment(db.Model):
      id = db.Column(db.Integer(), primary_key=True)
      user_id = db.Column(db.Integer(), db.ForeignKey('user.id'))
      ticket_status = db.Column(db.Enum(TicketStatus, name='ticket_status', default=TicketStatus.UNUSED))
      departure_time = db.Column(db.Date)
  </code></pre>
  
  <p>I want to change the value from all <code>ticket_status</code> after <code>datetime.utcnow()</code> passed the date value from <code>departure_time</code>.</p>
  
  <p>I tried to code like this:</p>
  
  <pre><code>class TicketStatus(enum.Enum):
      UNUSED = 'UNUSED'
      USED = 'USED'
      EXPIRED = 'EXPIRED'
  
      def __repr__(self):
          return str(self.value)
  
  
  class Payment(db.Model):
      id = db.Column(db.Integer(), primary_key=True)
      user_id = db.Column(db.Integer(), db.ForeignKey('user.id'))
      ticket_status = db.Column(db.Enum(TicketStatus, name='ticket_status', default=TicketStatus.UNUSED))
      departure_time = db.Column(db.Date)
  
      # TODO | set ticket expirations time
      def __init__(self):
          if datetime.utcnow() &gt; self.departure_time:
              self.ticket_status = TicketStatus.EXPIRED.value
          try:
              db.session.add(self)
              db.session.commit()
          except Exception as e:
              db.session.rollback()
  </code></pre>
  
  <p>I also tried like this:</p>
  
  <pre><code>def ticket_expiration(self, payment_id):
      now = datetime.utcnow().strftime('%Y-%m-%d')
      payment = Payment.query.filter_by(id=payment_id).first()
      if payment.ticket_status.value == TicketStatus.USED.value:
          pass
      elif payment and str(payment.departure_time) &lt; now:
          payment.ticket_status = TicketStatus.EXPIRED.value
      elif payment and str(payment.departure_time) &gt;= now:
          payment.ticket_status = TicketStatus.UNUSED.value
      try:
          db.session.commit()
      except Exception as e:
          db.session.rollback()
      return str('ok')
  </code></pre>
  
  <p>But it seems no effect when the <code>datetime.utcnow()</code> passed the date value from <code>departure_time</code>.</p>
  
  <p>So the point of my questions is, how to change the value from a row automatically after a set of times..?</p>