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

Sriram Karra karra.etc at gmail.com
Tue Feb 12 02:43:51 CET 2013

On Wed, Feb 6, 2013 at 12:05 PM, Dhruv Baldawa <dhruvbaldawa at gmail.com>wrote:

> OK I misread. Let me see if I understood your problem correctly.
>    1. All the consultations will be stored in that table.
>    2. When you have a Consultation object, you need a `consultation_count`
>    property for the number of consultation for that day.
> So, this should work now, according to me:
> def generate_consultation_id(date):
>      return Consultation.query.filter_by(data=data).count() + 1
I assume you meant 'date=date' in the method body.

> class Consultation(Base):
>     ...
>     cid = Column(Integer, default=generate_consultation_id)
> This should work, according to me. The function is called while adding the
> id to the database.

Thanks a lot. It does appear to work. Just a clarification - I have two
sessions initialized - one for a demo database and one for a production
database and allow the users to switch between the two (for staff training,
or feature exploration purposes). I tested it briefly and the counts are
correctly incremented for whichever session is currently active. Just
wanted to confirm that behaviour will be consistent.

More information about the BangPypers mailing list