Hei Wes and Paul...

On 10/29/07, Wes Duff <[EMAIL PROTECTED]> wrote:
> Hey whats going on.
> I am a new sqlalchemist as well but lets see if this helps any.
>
> This is how I am getting a list of all names that corrispond with my
> document names class.
>
> names = [ c.name for c in model.Document.select_by(param=param) ]
> So I am just collecting all the names from my Document table. I added
> select_by(param=param) if you want to find all the names that have to do
> with a parameter.
> names becomes a list
>
> I hope this helps a little.

I think you misunderstood Paul's question (which I'm also waiting for
response)...  :)

What Paul is asking is:  given this mapping:


mapper(User, users_table)

mapper(Address, addresses_table, properties={
    'user' : relation(User, backref='addresses', lazy=False)
})


We are able to know that Address class has a reference to User class
by introspecting its mapper's properties:

>>> q = session.query(Address)
>>> q.mapper.properties['user']
<sqlalchemy.orm.properties.PropertyLoader object at 0x013094D0>


However, if the relation is declared this way:

mapper(Address, addresses_table)
mapper(User, users_table, properties={
    'addresses' : relation(Address, backref='user', lazy=False)
})


... our introspection gives us:

>>> q = session.query(Address)
>>> q.mapper.properties['user']

Traceback (most recent call last):
  File "<pyshell#42>", line 1, in -toplevel-
    q.mapper.properties['user']
KeyError: 'user'


But Address actually has a user property...  :(


If we need to introspect both Address->user and User->addresses, one
solution is to not use the backref ... :


mapper(User, users_table, properties={
    'addresses' : relation(Address, lazy=False)})

mapper(Address, addresses_table, properties={
    'user' : relation(User, lazy=False)})


>>> q = session.query(User)
>>> q.mapper.properties['addresses']
<sqlalchemy.orm.properties.PropertyLoader object at 0x01313F70>

>>> q = session.query(Address)
>>> q.mapper.properties['user']
<sqlalchemy.orm.properties.PropertyLoader object at 0x013134F0>


BTW, one practical use of this introspection can be found at:

http://trac.turbogears.org/ticket/1582




> On Oct 29, 2007 11:21 AM, Paul Johnston <[EMAIL PROTECTED]> wrote:
> > Hi,
> >
> > How do I get a list of the relations a mapper has? I've been using
> mapper.properties, but have just realised this doesn't pick up backrefs.

/me too...    :)



> > Any ideas? Thanks,

I also want to know that...




Cheers,

Roger

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to