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

Sriram Karra karra.etc at gmail.com
Tue Feb 12 07:05:16 CET 2013

On Tue, Feb 12, 2013 at 9:08 AM, Dhruv Baldawa <dhruvbaldawa at gmail.com>wrote:

> 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
> functions<
> http://docs.sqlalchemy.org/en/rel_0_7/core/schema.html#context-sensitive-default-functions
> >
> def generate_consultation_id(context):
>      return
> Consultation.query.filter_by(date=context.current_parameters['date']).count() +
> 1

Hey, I think I spoke too early and in error. I was explicitly setting the
cid field and not hitting the default setter, and I mistakenly thought the
solution actually worked. I keep getting the error:

sqlalchemy.exc.StatementError: type object 'Consultation' has no attribute
'query' (original cause: AttributeError: type object 'Consultation' has no
attribute 'query') 'INSERT INTO consultation (patient_id, doctor_id,
dept_id, date, charge, notes, cid) VALUES (?, ?, ?, ?, ?, ?, ?)' [{'notes':
'Lorem Ipsum Gypsum zero sum', 'patient_id': 1, 'date': datetime.date(2013,
1, 16), 'charge': 30, 'dept_id': 7, 'doctor_id': None}]

query appears to be an attribute of a session class. Know what's going on?

More information about the BangPypers mailing list