[Twisted-Python] enterprise.row and two-way relationships

Ken Kennedy kkennedy at abelard.kenzoid.com
Sun Oct 20 14:50:58 EDT 2002

On Sat, Oct 19, 2002 at 06:21:32PM -0500, Justin Ryan wrote:
> Hello..
> To solve this problem, I've got a 'rel' table which keeps relationships
> between account ids and contact ids, so if I want all of the contacts
> for account x I simply pull all of the rows out of the 'rel' table whose
> accountId is x, and then individually step through them and extract the
> contacts with the corresponding accountIds..
> I don't think there is a problem with the way I'm doing this, or a
> better way to do this (though I'd be tickled if someone could point me a
> better way), but in any case there does not seem to be an (obvious) way
> to handle this with RowObjects..

It's a little unclear, but the "pull all of the rows out..and then
individually step through them" seems to suggest you're making two
queries to the db. If that's the case,then yeah, there's a better
way. If you know the accountID (from some previous query), just join
rel to contacts:

"select contact_info from contacts inner join rel on (rel.contactid =
contacts.contactid) where rel.accountid = whatever" 

If you only know account name info, use:

"select contact_info from 
       accounts inner join rel on (rel.accountid = accounts.accountid)
       inner join
       contacts on (contacts.contactid=rel.contactid)
       where account.accountname = 'whatever'

Either way, you should be getting back what you need in one
query. Does that make sense?


More information about the Twisted-Python mailing list