Re: [sqlobject] [SQLObject] Table Joining to Itself
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: Victor Ng <vn...@sy...> - 2004-03-25 13:58:48
|
Similiarly - if you wanted to have Many to Many relationships, you can use: parents = RelatedJoin('prodcats', addRemoveName='Parent', intermediateTable='prodcats_parent_child', otherColumn = 'parent_id', joinColumn='child_id') children = RelatedJoin('prodcats', addRemoveName='Child', intermediateTable='prodcats_parent_child', otherColumn= 'child_id', joinColumn='parent_id') Just remember to use ifNotExists when you create the table - since you run the risk of creating the intermediateTable twice. prodcats.createTable(ifNotExists = True) vic On Mar 24, 2004, at 9:20 AM, David McNab wrote: > Hi, > > I'm posting here in the hope that my problem, and its solution, might > save others from the battle I've had. > > Basically, I'm implementing a web shopping cart. While I was tempted > to have just a plain 2 or 3 level product hierarchy, this felt too > restrictive and I wanted a more general solution - the ability to have > an unrestricted n-level hierarchy of product categories. > > After some experimenting and reading SQLObject source, the following > solution came out: > > class prodcats(SQLObject): > _connection = _conn > name=StringCol() > longname=StringCol() > parent=ForeignKey('prodcats', default=None) > children=MultipleJoin('prodcats', joinColumn='parent_id') > > prodcats.createTable() > > rPets = prodcats.new( > name='pets', longname='Pets') > > rBirds = prodcats.new( > name='birds', longname='Birds', parent=rPets) > > rLargeBirds = prodcats.new( > name='large', longname='Large Birds', parent=rBirds) > > rSmallBirds = prodcats.new( > name='small', longname='Small Birds', parent=rBirds) > > rDogs = prodcats.new( > name='dogs', longname='Dogs', parent=rPets) > > rLargeDogs = prodcats.new( > name='large', longname='Large Dogs', parent=rDogs) > > rSmallDogs = prodcats.new( > name='small', longname='Small Dogs', parent=rDogs) > > -- > > Kind regards > David > > -- > > > ------------------------------------------------------- > This SF.Net email is sponsored by: IBM Linux Tutorials > Free Linux tutorial presented by Daniel Robbins, President and CEO of > GenToo technologies. Learn everything from fundamentals to system > administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click > _______________________________________________ > sqlobject-discuss mailing list > sql...@li... > https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss > > --- "We are what we repeatedly do. Excellence then, is not an act. It is a habit." - Aristotle |