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

Sriram Karra karra.etc at gmail.com
Wed Feb 6 07:17:52 CET 2013


On Wed, Feb 6, 2013 at 11:32 AM, Dhruv Baldawa <dhruvbaldawa at gmail.com>wrote:

> 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.
>

If I read your code correctly this would mean:

(a) the consultation_count property for all the consutlations that happened
on a given day will be the same value
(b) for a given row this value will keep changing as we insert more records
into the table.

So this will not work, no?


More information about the BangPypers mailing list