[sqlalchemy] Re: How to get list of relations
Hi, You are missing a compile call before you can iterate properties. try adding: Ah, that is indeed the problem. With that in place, iterate_properties does very nearly what I need. The only problem is that I need to get the name of the relation as well. For now, the following works: [(a,b) for a,b in getattr(obj.mapper, '_Mapper__props').items() if isinstance(b, sqlalchemy.orm.properties.PropertyLoader)] There's probably a better way, but my app is now working. Paul --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: How to get list of relations
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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: How to get list of relations
On Oct 30, 2007, at 10:16 AM, Roger Demetrescu wrote: If we need to introspect both Address-user and User-addresses, one solution is to not use the backref ... : use mapper.get_property(name) and mapper.iterate_properties(). I've considered removing properties as a public accessor since it serves no useful purpose. --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: How to get list of relations
Mike, use mapper.get_property(name) and mapper.iterate_properties(). I've considered removing properties as a public accessor since it serves no useful purpose. This doesn't work for me - the following code outputs: [Column('id', Integer(), primary_key=True, nullable=False)] [Column('val', String(length=None,convert_unicode=False))] I can do a test case without Elixir if needed, but I don't think that will change the result. from sqlalchemy import * from elixir import * __metadata__ = MetaData('mssql://./test') class Paj(Entity): val = Field(String) class Bob(Entity): paj = ManyToOne(Paj, primary_key=True, backref='bob') silly = Field(Integer) for a in Paj.mapper.iterate_properties: print a.columns Paul --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: How to get list of relations
Hi, use mapper.get_property(name) and mapper.iterate_properties (). I've considered removing properties as a public accessor since it serves no useful purpose. Ok, I found a hacky way that does what I need: [(n, getattr(obj, n)) for n in dir(obj) if isinstance(getattr(obj, n), sqlalchemy.orm.attributes.InstrumentedAttribute)] That'll do me for now. Paul --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: How to get list of relations
Hi, Ok, I found a hacky way that does what I need: [(n, getattr(obj, n)) for n in dir(obj) if isinstance(getattr(obj, n), sqlalchemy.orm.attributes.InstrumentedAttribute)] Ooops, not quite what I need. How do I go from a CollectionAttributeImpl to a mapper? Paul --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: How to get list of relations
On Oct 30, 2007, at 11:25 AM, Paul Johnston wrote: Mike, use mapper.get_property(name) and mapper.iterate_properties (). I've considered removing properties as a public accessor since it serves no useful purpose. This doesn't work for me - the following code outputs: [Column('id', Integer(), primary_key=True, nullable=False)] [Column('val', String(length=None,convert_unicode=False))] I can do a test case without Elixir if needed, but I don't think that will change the result. from sqlalchemy import * from elixir import * __metadata__ = MetaData('mssql://./test') class Paj(Entity): val = Field(String) class Bob(Entity): paj = ManyToOne(Paj, primary_key=True, backref='bob') silly = Field(Integer) for a in Paj.mapper.iterate_properties: print a.columns what is it youre looking for ? those columns are associated with ColumnProperty objects associated with your mapper. you want to filter out and get just the PropertyLoaders, those correspond to relation(). --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: How to get list of relations
On 10/30/07, Paul Johnston [EMAIL PROTECTED] wrote: Mike, use mapper.get_property(name) and mapper.iterate_properties (). I've considered removing properties as a public accessor since it serves no useful purpose. This doesn't work for me - the following code outputs: [Column('id', Integer(), primary_key=True, nullable=False)] [Column('val', String(length=None,convert_unicode=False))] I can do a test case without Elixir if needed, but I don't think that will change the result. from sqlalchemy import * from elixir import * __metadata__ = MetaData('mssql://./test') class Paj(Entity): val = Field(String) class Bob(Entity): paj = ManyToOne(Paj, primary_key=True, backref='bob') silly = Field(Integer) You are missing a compile call before you can iterate properties. try adding: Paj.mapper.compile() But maybe that should be done automatically in iterate_properties. for a in Paj.mapper.iterate_properties: print a.columns -- Gaƫtan de Menten http://openhex.org --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sqlalchemy] Re: How to get list of relations
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. 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. Any ideas? Thanks, Paul --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---