Hi,

Do you need to store expiry_code? seeing as it is a function of
last_con and the current date.

class PhNumber(Base):
  __tablename__ = 'ph_numbers'
  ph_no = Column(Integer, nullable=False)
  last_con = Column(DateTime, nullable=False)

  @property
  def expiry_code(self):
    msg = 'Expired 3 months'
    now = datetime.datetime.now()
    if now > (self.last_con  - 90):
        return msg
    return 'Not Applicable'

If the column needs to be queried from outside sqlalchemy, then you
could put the logic in a database function (depending upon what
database you are using).



On Mar 1, 12:52 pm, dalia <[email protected]> wrote:
> Hi,
>
> I have a table of phone numbers which has 3 columns named -
>
> 1. ph_no  Integer not null
> 2. last_contacted Datetime not null
> 3. expiry_code Text()
>
> The behaviour of the table should be - When the last_contacted column
> has a date which is 3 months older, the expiry_code column should have
> the value 'number expired'. I'm not sure how this can be done using
> declarative method. I did the following -
>
> class PhNumber(Base):
>     __tablename__ = 'ph_numbers'
>     ph_no = Column(Integer, nullable=False)
>     last_con = Column(DateTime, nullable=False)
>     expiry_code = Column(Text(), default=mydefault,
> onupdate=mydefault)
>
> def mydefault(context):
>     msg = 'Expired 3 months'
>     now = datetime.datetime.now()
>     if now > (context.current_parameters['last_con']  - 90):
>         return msg
>     return 'Not Applicable'
>
> mydefault function calculates if the value in last_con column is
> greater than 3 months of today's date, it stores 'Expired 3 months' in
> expiry_code. But this happens only when a new insert or update occurs
> in this table.
>
> I want the value in expiry_code to be changed even without any update/
> insert operations on the table. Whenever the table is selected, the
> updated value should be shown. Is this possible in SQLAlchemy? Please
> let me know.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to