Hi Calum,

On 01/13/2012 01:31 PM, Calum MacLeod wrote:
Thanks, Ian,

I appreciate your advice and have removed the first conditional (if name not in duties).

Have now changed that to:

    duty = Duty(date=date)
    duties[date] = duty

    if name not in volunteers:
        volunteer = Volunteer(fore=fore, surn=surn, name=name)
        volunteers[name] = volunteer
        volunteer.duties.append(duty)
Another hobbiest here (so take the following with a grain of salt), I think you would want this:

else:
    volunteers[name].duties.append(duty)

Or

    if name not in volunteers:
        volunteer = Volunteer(fore=fore, surn=surn, name=name)
        volunteers[name] = volunteer

    volunteers[name].duties.append(duty)


Would also change your Volunteer definition from:

class Volunteer(Base):
    __tablename__ = 'volunteers'
    id = Column(Integer, primary_key=True)
    fore = Column(String)
    surn = Column(String)
    name = Column(String)
    dates = association_proxy('duties', 'date')

to:

class Volunteer(Base):
    __tablename__ = 'volunteers'
    id = Column(Integer, primary_key=True)
    fore = Column(String)
    surn = Column(String)
    dates = association_proxy('duties', 'date')

      # read only columns
    @hybrid_property
    def name(self):
        return self.fore + ', ' + self.surn

I.e. don't store the same information in two different columns - why not is better explained on e.g. the wiki - http://en.wikipedia.org/wiki/Database_normalization

"hybrid_property" is described here http://www.sqlalchemy.org/docs/orm/extensions/hybrid.html?highlight=hybrid_property#sqlalchemy.ext.hybrid.hybrid_property

If you do this you would need to change this:

volunteer = Volunteer(fore=fore, surn=surn, name=name)

to:
volunteer = Volunteer(fore=fore, surn=surn)

Hope this helps
Werner

--
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to