[sqlalchemy] Association Proxy question ( or other ORM feature )

2013-08-06 Thread Jonathan Vanasco
I have three tables that are structured somewhat like this:

Document
* id 
* all_images = sa.orm.relationship(Document2Image)

Image
* id
* all_documents = sa.orm.relationship(Document2Image)

Document2Image
* id
* document_id
* image_id
* image_type_id
* document = sa.orm.relationship(Document)
* image = sa.orm.relationship(Image)



is there a convenient way to set up a relationship on Document where it 
maps to Document2Image with a specific Document2Image.image_type_id ?

I believe I want a variant of the Association Proxy [ 
http://docs.sqlalchemy.org/en/rel_0_8/orm/extensions/associationproxy.html 
] that was listed in Relationships - Association Object [ 
http://docs.sqlalchemy.org/en/rel_0_8/orm/relationships.html#association-object 
]

I just can't wrap my head around this.

What I'd like to have is something like :

Document
* id 
* all_images = sa.orm.relationship(Document2Image)
* header_image = sa.orm.relationship( Image , filter( Image.image_type_id 
== 1 , Image.id == Document2Image.image_id , Document2Image.document_id == 
Document.id )

this way i can just write out :

   document.header_image

and either see 'None' or the 'Image' object.

if it matters, i'm only concerned with reads right now.

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.




Re: [sqlalchemy] Association Proxy question ( or other ORM feature )

2013-08-06 Thread Michael Bayer

On Aug 6, 2013, at 6:23 PM, Jonathan Vanasco jonat...@findmeon.com wrote:

 I have three tables that are structured somewhat like this:
 
   Document
   * id 
   * all_images = sa.orm.relationship(Document2Image)
 
   Image
   * id
   * all_documents = sa.orm.relationship(Document2Image)
 
   Document2Image
   * id
   * document_id
   * image_id
   * image_type_id
   * document = sa.orm.relationship(Document)
   * image = sa.orm.relationship(Image)
 
 
 
 is there a convenient way to set up a relationship on Document where it maps 
 to Document2Image with a specific Document2Image.image_type_id ?
 
 I believe I want a variant of the Association Proxy [ 
 http://docs.sqlalchemy.org/en/rel_0_8/orm/extensions/associationproxy.html ] 
 that was listed in Relationships - Association Object [ 
 http://docs.sqlalchemy.org/en/rel_0_8/orm/relationships.html#association-object
  ]

you can build a relationship from Document to Document2Image using a custom 
primaryjoin, then relationship to Image, then association proxy.

Or, you can produce a select() of what rows you want from Document2Image, then 
make that as the secondary argument between a single relationship on 
Document-Image.

If OTOH you want a join condition that uses columns from Document2Image and 
Image at the same time, sometimes you can make a select() of those two things, 
then create a non primary mapper to Image using that select() as the base, then 
Document gets a relationship referring to that non-primary mapper.




signature.asc
Description: Message signed with OpenPGP using GPGMail