This if my first post so let me first just say spectacular job to 
everyone involved.  Coming from SQLObject, I was first turned off by the 
the complexity but have grown to really like the flexibility (and the 
great documentation)

I've created a mapper recently that looks to my untrained eye that it is 
much more verbose than necessary.  Here is the code, noting the 
association_table mapper near the end:

###############################################
from sqlalchemy import *
db = create_engine('sqlite:///myeme.db')
metadata = BoundMetaData(db)

person_table = Table('person', metadata,
                      Column('person_id', Integer, primary_key=True),
                      Column('user_name', String(40))
                      )
class Person(object):
     pass
mapper(Person, person_table)

association_table = Table('association', metadata,
                           Column('source_id', Integer, 
ForeignKey("person.person_id"), primary_key=True),
                           Column('object_id', Integer, 
ForeignKey("person.person_id"), primary_key=True),
                           Column('Comment', String(128))
                          )
class Association(object):
     pass
mapper(Association, association_table, properties={
     'source':relation(Person, 
primaryjoin=association_table.c.source_id==person_table.c.person_id,
                       backref=backref("opinions", 
primaryjoin=association_table.c.source_id==person_table.c.person_id, 
cascade="all, delete-orphan")),
     'object':relation(Person, 
primaryjoin=association_table.c.object_id==person_table.c.person_id,
                       backref=backref("opinioned", 
primaryjoin=association_table.c.object_id==person_table.c.person_id, 
cascade="all, delete-orphan")),
})
#######################################################

I've had to create four primaryjoin statements yet all of the pertinent 
information is contained in the table declaration.  I understand the 
initial ambiguity as I have two foreign relations to my "Person" table 
and the mapper can't resolve which one is the "source" and which is the 
"object".  However shouldn't we be able to assign a simple 
"OnColumn=THIS" (I am making up the name since I can't find it in the 
docs) which means "we know it is ambiguous because two columns have the 
same foreignkey, but we really mean THIS column".  It isn't as much the 
extra typing of writing a primaryjoin but that I want to make the table 
definition more authoritative (ie I don't want to change the foreignkey 
in the table then have to remember to change the mapper to match). 
Does this exist?  Would it make a good feature if it doesn't?

Also why does the backref need a primaryjoin?  We've already resolved 
the ambiguity with the forward reference so why can't it just follow it 
backward to the proper column?

Thanks for any help, suggestions or thoughts anyone can provide.  I am a 
bit of a DB newbie so my apologies if I am missing something obvious. 
Again great work to everyone involved.

-brian

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Sqlalchemy-users mailing list
Sqlalchemy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to