Hi all,

I've found a problem I've hade the chance to circumvent it but would 
like to comment it out here in order to get some feedback.

the background (that will be a recipe in the wiki):

I use to use RelatedJoins to simulate one-to-many relations without the 
pain to add a foreignkey field for each relation.
Consider having a Telephone class and several clases that are naturally 
many-to-one related with telephone numbers. The tipical one-to-many 
aproach is:

class Telephone(SQLObject):
    number = StringCol()
    type = EnumCol(enumValues=['fax', 'line', 'movil'])
    person = ForeignKey('Person')
    entity = ForeignKey('Entity')

class Person(SQLObject):
    name = StringCol()
    t = MultipleJoin('Telephone')

class Entity(SQLObject):
    name = StringCol()
    t = MultipleJoin('Telephone')

So we have an ugly telephones table with two fk columns that should be 
mutually exclusive at a higher abstraction level than sql, but nothing 
constraints us to create a

Telephone(number = '987123546', type = 'fax', person = 2, entity = 3)

which is an incongruence in that higher than sql abstraction level.

So I prefer to make it this way:

class Telephone(SQLObject):
    number = StringCol()
    type = EnumCol(enumValues=['fax', 'line', 'movil'])
    person = RelatedJoin('Person')
    entity = RelatedJoin('Entity')

class Person(SQLObject):
    name = StringCol()
    t = RelatedJoin('Telephone')

class Entity(SQLObject):
    name = StringCol()
    t = RelatedJoin('Telephone')

Now the incongruence could be that a person (or entity) can have many 
telephones but also a telephone can belong to several persons/entities. 
At least, with third tables to store the relations we will not have one 
(or more) fk column empty always in all telephone registers.

I've gone a little step beyond and found that I can define the 
RelatedJoin only in the class I'm willing to use it. That is:

class Telephone(SQLObject):
    number = StringCol()
    type = EnumCol(enumValues=['fax', 'line', 'movil'])

class Person(SQLObject):
    name = StringCol()
    t = RelatedJoin('Telephone')

class Entity(SQLObject):
    name = StringCol()
    t = RelatedJoin('Telephone')


so I have Person.addTelephone() but dont Telephone.addPerson(), that is 
a way to avoid the incongruences of using a many-to-many mechanism to 
play many-to-one.

Finally, the problem:

When defining simple related joins as above, the first of the two 
classes in alphabetical order is responsible of creating the related 
table. That is, Entity class and Person class will create respectively 
the tables person_telephone and entity_telephone. If I had a

class Z(SQLObject):
    t = RelatedJoin('Telephone')

without the corresponding related join in class Telephone, the related 
table (in this case telephone_z) will not be created.

It won't also make it work:

class Z(SQLObject):
    t = RelatedJoin('Telephone')
    Telephone.sqlmeta.addJoin(RelatedJoin('Z'))

So I have two questions.

first is, shouldn't it work with the last class definition I've shown?

second, could SQLObject create the table on first RelatedJoin it found, 
instead of on the one resulting on applying the alphabetical sort?

cheers,
jonhattan

ps: please if something is not well expresed tell me and I'll made the 
effort to explain myself better. English is not my mother lang.











-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
sqlobject-discuss mailing list
sqlobject-discuss@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss

Reply via email to