|
From: <ab...@ne...> - 2005-06-03 09:06:05
|
I've discovered what seems to be a bug in sqlobject class inheritance.
An inherited class whose parent defines a foreign key, can only use that =
foreign
key/pointer if they themselves define a data attribute. If the inherited=
class
does not contribute a new data attribute, attempting to use the foreign k=
ey
(which is defined by the parent) will generate an attribute error.
P.S. You need to grab the latest source from the subversion repository in=
order
to get the InheritableSQLObject class. Release 0.6.1 doesn't come with it=
.
#----------------------------------------------
from sqlobject import *
from sqlobject.inheritance import InheritableSQLObject
from sqlobject.sqlite import builder; SQLiteConnection =3D builder()
conn =3D SQLiteConnection('persontest3.db', debug=3DFalse)
class Cubicle(SQLObject):
_connection =3D conn
location =3D StringCol()
occupiedby =3D ForeignKey('Person', default=3DNone) # and subclasses
class Person(InheritableSQLObject):
_connection =3D conn
cubicle =3D ForeignKey('Cubicle')
class Employee(Person):
_connection =3D conn
_inheritable =3D False # comment this line to sol=
ve bug OR
#position =3D StringCol(default=3D'Chief') # uncomment this line to=
solve bug
=20
# tests =20
Cubicle.dropTable(True)
Cubicle.createTable()
Person.dropTable(True)
Person.createTable()
Employee.dropTable(True)
Employee.createTable()
c =3D Cubicle(location=3D'first floor')
c.occupiedby =3D Employee(cubicle=3Dc)
# The following will crash with the error
# 'Employee' object has no attribute '_SO_val_cubicleID'
print 'c.occupiedby.cubicle', c.occupiedby.cubicle # BOOM!
#----------------------------------------------
Admittedly, most people using inherited classes want to define extra attr=
ibutes
- that's usually the whole point of an inherited class. So they would no=
t run
into this problem. However in my situation not all my inherited classes =
define
extra attributes (e.g. they instead have different behaviour etc.) so it =
is a
real problem for me.
-Andy Bulka
www.atug.com/andypatterns
------------------------------------------------------------
This email was sent from Netspace Webmail: http://www.netspace.net.au
|