Dear Werner,

Thanks for completing the puzzle for me!
I followed your code precisely and it worked as you would have expected.
So now I have the @hybrid property in my class definition too.

(The reason that I ended up previously with fore,surn and name was that I was not sure of how the line
volunteers[name] = volunteer
might be affected if I had fore,surn in there instead.)

Basically, I am just taking baby steps and getting the entire code to work gives me the confidence to progress.
Having code that works is great.

Thanks for your help.

Calum

On 13/01/12 16:31, werner wrote:
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.

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