[BangPypers] SQLAlchemy and 'non-trivial' default values for a column
Dhruv Baldawa
dhruvbaldawa at gmail.com
Wed Feb 6 07:35:05 CET 2013
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
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.
Reference<http://docs.sqlalchemy.org/en/rel_0_8/core/schema.html#sqlalchemy.schema.Column.__init__>
--
Dhruv Baldawa
(http://www.dhruvb.com)
On Wed, Feb 6, 2013 at 11:47 AM, Sriram Karra <karra.etc at gmail.com> wrote:
> 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?
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>
More information about the BangPypers
mailing list