On Thu, 2003-05-29 at 21:07, Edmund Lian wrote:
> 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'
That's a weird error. It looks like it's instantiating a
ComponentCategory object, but hitting an excepting when the object is
printed.
Hmm... looks like I wasn't handling None there properly -- instead of
returning None it was returning a broken ComponentCategory object. Try
CVS again.
Note that the actual ComponentCategory instance will never be in the
__dict__ of the category object -- only the id is stored.
> 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
It expects an id or a ComponentCategory object -- since you didn't pass
an object, it tried to treat it like an integer id.
Anyway, try it again from CVS and see if it works for you now.
Ian
|