Ian Bicking wrote:
> It should be an attribute category, which is categoryId with the "Id"
> removed.
Hmmm... I think there's a bug then. Here's what I'm seeing...
My SQLObject module is:
from SQLObject import *
__connection__ = PostgresConnection(host="localhost", db="sqlobj",
user="sqlobj", passwd="sqlobj")
class ComponentCategory(SQLObject):
_table = "st_component_category"
_columns = [
StringCol("name", length=100),
StringCol("description", default=None),
IntCol("sequenceNum", default=None)]
class Component(SQLObject):
_table = "st_component"
_columns = [
StringCol("name"),
StringCol("categoryId", foreignKey="ComponentCategory",
default=None),
IntCol("sequenceNum", default=None)]
ComponentCategory.createTable(ifNotExists=1)
Component.createTable(ifNotExists=1)
My test run and output is:
Python 2.2.2 (#1, Mar 21 2003, 23:01:54)
[GCC 3.2.3 20030316 (Debian prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from test import *
>>> cc = ComponentCategory.new(name="Test Cat")
>>> cc
<ComponentCategory 1 name='Test Cat' description=None sequenceNum=None>
>>> comp = Component.new(name='Test Comp')
>>> comp
<Component 1 name='Test Comp' categoryId=None sequenceNum=None>
>>> comp.__dict__
{'_SO_val_categoryId': None, '_SO_writeLock': <thread.lock object at
0x821c728>, '_SO_val_sequenceNum': None, '_SO_val_name': 'Test Comp',
'id': 1}
>>> comp.category
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib/python2.2/site-packages/SQLObject/SQLObject.py", line
915, in __repr__
return '<%s %i %s>' \
AttributeError: 'ComponentCategory' object has no attribute 'id'
Note that there is no category attribute. Unless I'm misreading the
docs, I would expect that instantiating a new Component but not
specifying a category or category ID should result in an instance with
an attribute "category" with value None.
Also, I would actually expect to instantiate Component and specify a
category by name (or more inconveniently, pass in an instance of it I
guess) rather than ID.
Passing in a string gives:
>>> comp = Component.new(name='Test Comp2', category='Test Cat')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib/python2.2/site-packages/SQLObject/SQLObject.py", line
747, in new
kw[column.name] = getID(kw[column.foreignName])
File "/usr/lib/python2.2/site-packages/SQLObject/SQLObject.py", line
1059, in getID
return int(obj)
ValueError: invalid literal for int(): Test Cat
Pass in an instance of ComponentCategory gives:
>>> cc
<ComponentCategory 1 name='Test Cat' description=None sequenceNum=None>
>>> comp = Component.new(name='Test comp 3', category=cc)
>>> comp
<Component 3 name='Test comp 3' categoryId='1' sequenceNum=None>
>>> comp.__dict__
{'_SO_val_categoryId': '1', '_SO_writeLock': <thread.lock object at
0x82db400>, '_SO_val_sequenceNum': None, '_SO_val_name': 'Test comp 3',
'id': 3}
>>> comp.category
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib/python2.2/site-packages/SQLObject/SQLObject.py", line
918, in __repr__
' '.join(['%s=%s' % (name, repr(value)) for name, value in
self._reprItems()]))
TypeError: an integer is required
Here is an example of what users probably don't expect to do--deal with
object IDs:
>>> comp = Component.new(name='Test comp 3', categoryId=1)
>>> comp
<Component 2 name='Test comp 3' categoryId='1' sequenceNum=None>
Notice that the attribute "category" is still missing:
>>> comp.__dict__
{'_SO_val_categoryId': '1', '_SO_writeLock': <thread.lock object at
0x8210ee8>, '_SO_val_sequenceNum': None, '_SO_val_name': 'Test comp 3',
'id': 2}
...Edmund.
|