Problem with InheritableSQLObject

6 views
Skip to first unread message

kerinin

unread,
Jan 9, 2007, 9:03:47 PM1/9/07
to TurboGears
I'm having a problem with inheritable sqlobjects. I have two classes,
one of which is a subclass of another, and i want to be able to
MultipleJoin to both of them - like so:

-----------------------
class BaseClass(InheritableSQLObject):
reference = ForeignKey('RefClass', default=None)

class OtherClass(BaseClass):
reference = ForeignKey('RefClass', default=None)

class RefClass(SQLObject):
base_classes = MultipleJoin('BaseClass',joinColumn='reference_id')
other_classes = MultipleJoin('OtherClass',joinColumn='reference_id')
----------------------

the problem is that instances of OtherClass are showing up in
RefClass.base_classes and NOT in RefClass.other_classes, as they
should.

Any ideas?

Leandro Lucarella

unread,
Jan 9, 2007, 10:28:30 PM1/9/07
to turbo...@googlegroups.com
kerinin, el 10 de enero a las 02:03 me escribiste:

I'm not really sure but I think you shouldn't provide the reference twice.
It's enough to provide it on the BaseClass only.

--
Leandro Lucarella (luca) | Blog colectivo: http://www.mazziblog.com.ar/blog/
.------------------------------------------------------------------------,
\ GPG: 5F5A8D05 // F8CD F9A7 BF00 5431 4145 104C 949E BFB6 5F5A 8D05 /
'--------------------------------------------------------------------'
Bald men with no jobs and no money who live with their parents don't approach
strange women.
-- George Constanza

Diez B. Roggisch

unread,
Jan 10, 2007, 7:15:08 AM1/10/07
to turbo...@googlegroups.com

You can't do that this way. reference can only be used once. Rename to e.g.

other_class_reference

and then create a property on OtherClass with name reference, that does the
getting and setting. Maybe that helps, I didn't test it though.

Diez

kerinin

unread,
Jan 10, 2007, 9:48:01 AM1/10/07
to TurboGears
That helps:

------------------
class BaseClass(InheritableSQLObject):
base_reference = ForeignKey('RefClass', default=None)

def _get_reference(self):
return self.base_reference
def _set_reference(self,value):
self.base_reference = value

class OtherClass(BaseClass):
other_reference = ForeignKey('RefClass', default=None)

def _get_reference(self):
return self.other_reference
def _set_reference(self,value):
self.other_reference = value

class RefClass(SQLObject):
base_classes =
MultipleJoin('BaseClass',joinColumn='base_reference_id')
other_classes =
MultipleJoin('OtherClass',joinColumn='other_reference_id')
-------------------

this produces the correct behavior if instances of OtherClass are
created without the reference parameter and then the reference
parameter is set after instantiation, but the problem still exists if
you do something like this:

ref=RefClass()
other=OtherClass(reference=ref)

It doesn't seem to use the overloaded get and set methods when creating
the object.

Diez B. Roggisch

unread,
Jan 10, 2007, 11:32:11 AM1/10/07
to turbo...@googlegroups.com

You can override the _create-method in both classes and map reference to the
respective property that is really needed.

Diez

kerinin

unread,
Jan 10, 2007, 1:07:23 PM1/10/07
to TurboGears
That did it - Thank you so much. I've been looking for something like
the _create method, but couldn't find it. SQLObject's documentation is
really hit or miss once you get outside the basic feature set.

Here's my code for anyone having similar issues:

------------------------


class BaseClass(InheritableSQLObject):
base_reference = ForeignKey('RefClass', default=None)

def _get_reference(self):
return self.base_reference
def _set_reference(self,value):
self.base_reference = value

def _create(self,id,reference=None,**kargs):
super(BaseClass,self)._create(id,**kargs)
if reference:
self.reference = reference

class OtherClass(BaseClass):
other_reference = ForeignKey('RefClass', default=None)

def _get_reference(self):
return self.other_reference
def _set_reference(self,value):
self.other_reference = value

def _create(self,id,reference=None,**kargs):
super(OtherClass,self)._create(id,**kargs)
if reference:
self.reference = reference

class RefClass(SQLObject):
base_classes =
MultipleJoin('BaseClass',joinColumn='base_reference_id')
other_classes =
MultipleJoin('OtherClass',joinColumn='other_reference_id')

--------------------------

Reply all
Reply to author
Forward
0 new messages