Self referencing class with SQLObject in TG

0 views
Skip to first unread message

mar...@gmail.com

unread,
Nov 14, 2005, 12:40:59 PM11/14/05
to TurboGears
Ok, maybe this would be better off in a SQLObject discussion list.
Sorry about that.

What I need to know is how to model a self-referencing relationship
using SQLObject.

Why? Let's say I want to model a father-son relationship. So I would
have a table in the database containing person data. Two of the
atributes of that table should be father and mother, that should
reference another record of the same table.

How to I model that with SQLObject.

Thanx in advance.
Marcos

Leandro Lucarella

unread,
Nov 14, 2005, 12:49:45 PM11/14/05
to turbo...@googlegroups.com
mar...@gmail.com, el lunes 14 de noviembre a las 17:40 me escribiste:
>
> Ok, maybe this would be better off in a SQLObject discussion list.
> Sorry about that.
>
> What I need to know is how to model a self-referencing relationship
> using SQLObject.
>
> Why? Let's say I want to model a father-son relationship. So I would
> have a table in the database containing person data. Two of the
> atributes of that table should be father and mother, that should
> reference another record of the same table.

I think this should work just fine:

class Person(SQLObject):
# Unless you want to have a DB with a philosophical
# problem I guess you should accept None =)
mother = ForeignKey('Person', default=None)
father = ForeignKey('Person', default=None)
# ...

Then:
father = Person(...)
mother = Person(...)
child = Person(..., mother=mother, father=father)

--
Leandro Lucarella (luca) | Blog colectivo: http://www.mazziblog.com.ar/blog/
.------------------------------------------------------------------------,
\ GPG: 5F5A8D05 // F8CD F9A7 BF00 5431 4145 104C 949E BFB6 5F5A 8D05 /
'--------------------------------------------------------------------'
aFR [a...@my.farts.cause.nuclear.reaction.org] has quit IRC (Ping timeout)

p

unread,
Nov 14, 2005, 2:13:42 PM11/14/05
to TurboGears
Yup, I've done this is well. Specifically, I've used RelatedJoins so
that you can have multiple parents or children:

class Person(SQLObject):
name = StringCol(alternateID=True, length=30)
child = RelatedJoin('People',intermediateTable='peopleparentchild',
joinColumn='parent',otherColumn='child')

Assign like this like this:
john = Person(name='john')
chris = Person(name='chris')
christina = Person(name='christina')
paul = Person(name='paul')
paula = Person(name='paula')

# Make chris and christina children of john
john.addPerson(chris)
john.addPerson(christina)

# Make paul and paul parents of john
paul.addPerson(name='john')
paula.addPerson(name='john')

mar...@gmail.com

unread,
Nov 14, 2005, 2:25:11 PM11/14/05
to TurboGears
I'm not sure, but I believe I tested this, and it doesn't work. If I
recall correctly when your creating the table it gives and error,
because the table person is not created yet. But I might have done
something wrong. I try it again.

Thanks

mar...@gmail.com

unread,
Nov 14, 2005, 2:28:18 PM11/14/05
to TurboGears
shouldn't it be

child = RelatedJoin('Person',intermediateTable='peopleparentchild',
joinColumn='parent',otherColumn='child') ?

Reply all
Reply to author
Forward
0 new messages