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

Sriram Karra karra.etc at gmail.com
Tue Feb 5 17:28:36 CET 2013


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


More information about the BangPypers mailing list