|
From: Edmund L. <el...@in...> - 2003-06-04 03:09:19
|
I've found a couple of bugs in RelatedJoin...
1. If you're going to explicitly define a class for the mapping table,
you cannot use .createTable() to create tables on either side of the
join. This is because the RelatedJoin declaration forces SQLObject to
try to create the mapping (intermediate) table again, and this causes an
exception.
2. When the mapping table table is used to map two or more many:many
relationships, the presence of null values in the mapping table causes
an exception. Example test case:
Schema
------
create table cat (
id serial,
name text
);
create table dog(
id serial,
name text
);
create table person(
id serial,
name text
);
create table owner_map(
id serial,
person_id integer,
cat_id integer,
dog_id integer
);
Classes
-------
class Dog(SQLObject):
_columns = [
StringCol("name")]
_joins = [
RelatedJoin("Person",
intermediateTable = "owner_map",
joinColumn = "dog_id",
otherColumn = "person_id")]
class Cat(SQLObject):
_columns = [
StringCol("name")]
_joins = [
RelatedJoin("Person",
intermediateTable = "owner_map",
joinColumn = "cat_id",
otherColumn = "person_id")]
class Person(SQLObject):
_columns = [
StringCol("name")]
_joins = [
RelatedJoin("Dog",
intermediateTable = "owner_map",
joinColumn = "person_id",
otherColumn = "dog_id"),
RelatedJoin("Cat",
intermediateTable = "owner_map",
joinColumn = "person_id",
otherColumn = "cat_id")]
class OwnerMap(SQLObject):
_columns = [
IntCol("personId"),
IntCol("dogId", foreignKey="Dog"),
IntCol("catId", foreignKey="Cat")]
How to raise the exception:
>>> d = Dog.new(name="Snoopy")
>>> c = Cat.new(name="Garfield")
>>> p = Person.new(name="John")
>>> p.addDog(d)
>>> p.dogs
[<Dog 2 name='Snoopy'>]
>>> p.cats
[Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib/python2.2/site-packages/SQLObject/SQLObject.py", line
925, in __repr__
return '<%s %i %s>' \
AttributeError: 'Cat' object has no attribute 'id'
>>>
What's happening (I think) is that the query that SQLObject uses to find
the right row in the mapping table is assuming that either no row is
found, or that if a row is found, then a not null value for cat_id will
exist. So the null/None value for cat_id in owner_map causes an exception.
I'd fix it, but I don't understand SQLObject well enough to do so. Sorry...
...Edmund.
|