[BangPypers] SQLAlchemy and 'non-trivial' default values for a column

Dhruv Baldawa dhruvbaldawa at gmail.com
Wed Feb 6 07:02:45 CET 2013


I would do a:

class Consultation(Base):
    __tablename__ = 'consultation'

    id         = Column(Integer, primary_key=True)
    patient_id = Column(Integer, ForeignKey('patient.id'))
    doctor_id  = Column(Integer, ForeignKey('doctor.id'))
    date       = Column(Date(),  default=MyT.today())

   @property
   def consultation_count(self):
        ''' returns the consultation count for current date '''
        return self.query.filter_by(date=self.date).count() # the syntax
might not be correct

c = Consultation.query.get(1)
print c.consultation_count

This way its computed on the fly and you dont need to store it.

--
Dhruv Baldawa
(http://www.dhruvb.com)


On Wed, Feb 6, 2013 at 11:15 AM, Vinod Kumar Narasimhaiah <
vinod.narasimhaiah at gmail.com> wrote:

> I am not a python programmer, but how about this logic?
>
> add new field called "consultation_count" to the same table
>
> before every insert to the table:
>
> Check if the date field on the last record (you might want to add a
> timestamp field to get the last record easily) = today's date
>
> If yes, then it's the same day-
>     take the consultation count from the last record, increment it by one
> and create the new record.
> If No, then it's a start of the new day-
>     set the counter to 1 and create the new record.
>
>
>
> On Tue, Feb 5, 2013 at 9:58 PM, Sriram Karra <karra.etc at gmail.com> wrote:
>
> > I have a Declarative table defined as follows:
> >
> > class Consultation(Base):
> >     __tablename__ = 'consultation'
> >
> >     id         = Column(Integer, primary_key=True)
> >     patient_id = Column(Integer, ForeignKey('patient.id'))
> >     doctor_id  = Column(Integer, ForeignKey('doctor.id'))
> >     date       = Column(Date(),  default=MyT.today())
> >
> > Each row in this table represents a single consultation instance of a
> > patient seeing a doctor on a given day.
> >
> > I would like an additional attribute called "cid" that should be an
> > auto-incrementing value representing how many-th consultation it was in
> > that day. Basically it is an auto-incrementing counter, which gets reset
> to
> > 0 at the start of a day (hence not unique, whereas the id will be
> unique).
> > No row is ever deleted.
> >
> > How do I do achieve this with the least amount of additional database
> > space? It is trivial to have another table with one column for date and
> > another column for the total consultations thus far.
> >
> > Any help?
> >
> > -Karra
> >
> > P.S. This is for PRS - an open source patient record system for small
> > clinics I am developing, and available at: https://github.com/skarra/PRS
> > _______________________________________________
> > BangPypers mailing list
> > BangPypers at python.org
> > http://mail.python.org/mailman/listinfo/bangpypers
> >
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>


More information about the BangPypers mailing list