[BangPypers] SQLAlchemy and 'non-trivial' default values for a column
dhruvbaldawa at gmail.com
Tue Feb 12 04:38:56 CET 2013
On Tue, Feb 12, 2013 at 7:13 AM, Sriram Karra <karra.etc at gmail.com> wrote:
> On Wed, Feb 6, 2013 at 12:05 PM, Dhruv Baldawa <dhruvbaldawa at gmail.com
> > 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
> > 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.
Yes, sorry for the typo.
> > class Consultation(Base):
> > ...
> > cid = Column(Integer, default=generate_consultation_id)
> > This should work, according to me. The function is called while adding
> > 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.
Yes, this will be consistent. Just to explain in brief how it works. The
default function gets executed in runtime, when a new record is created, it
just go throughs the database, finds the number of records for that
particular date and increments it by one and returns it. As long as the
same procedure is followed, it would be consistent, but if someone
specifies the value explicitly, it might create a problem. Also, I have
found a bug which can be solved using context-based default
> BangPypers mailing list
> BangPypers at python.org
More information about the BangPypers