How to find one-to-many relations of a class?

29 views
Skip to first unread message

AlexVhr

unread,
Dec 12, 2012, 4:27:48 AM12/12/12
to sqlal...@googlegroups.com
Hi all.
I need to find out if a certain class has one-to-many relations to other classes, and what exactly this classes are. Something like this:

class Parent(Base):

    __tablename__ = 'parent'

    id = Column(Integer, primary_key=True)

    children = relationship("Child")


class Child(Base):

    __tablename__ = 'child'

    id = Column(Integer, primary_key=True)

    parent_id = Column(Integer, ForeignKey('parent.id'))


I'm kind of half way there:


def findRelatedClasses(cls):

    #iterate cls.__dict__, find a class member with

    #property.direction.name == ‘ONETOMANY’.

    #Now, to find out which class it links to…

 

findRelatedClasses(Parent) should return a list of type objects ([Child] in this instance). Any ideas how to do that? Thanks!

Juan Hernandez

unread,
Dec 12, 2012, 7:41:51 AM12/12/12
to sqlal...@googlegroups.com
I'm not sure I get your question...

if you have this already

children = relationship("Child")

what is it that you need to know?? 

By using MyClass.children you'll have access to it's children and they will be, one2many relationships




--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To view this discussion on the web visit https://groups.google.com/d/msg/sqlalchemy/-/LaLdjo3_XoUJ.
To post to this group, send email to sqlal...@googlegroups.com.
To unsubscribe from this group, send email to sqlalchemy+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.

AlexVhr

unread,
Dec 12, 2012, 9:05:24 AM12/12/12
to sqlal...@googlegroups.com
I have "this allready" at compile-time. But at run-time I will have to deal with instances of different classes dinamically. So I need to inspect the instances to find out if they are linked to others or not, and what is that they are linked to. Something like this:

def print_related_records(instance):
   related_classes = find_related_classes(instance.__class__) #returns  {'children':Child}
   #iterate the related_classes dict, query all the related records, then print them
   ...

john = Parent('John Smith')
betty = Child('Betty')
john.children.append(betty)
...

print_related_records(john) #prints 'Betty'
print_related_records(betty) #prints nothing

Michael Bayer

unread,
Dec 12, 2012, 11:11:55 AM12/12/12
to sqlal...@googlegroups.com
On Dec 12, 2012, at 4:27 AM, AlexVhr wrote:

Hi all.
I need to find out if a certain class has one-to-many relations to other classes, and what exactly this classes are. Something like this:
I'm kind of half way there:


def findRelatedClasses(cls):

    #iterate cls.__dict__, find a class member with

    #property.direction.name == ‘ONETOMANY’.

    #Now, to find out which class it links to…

 

findRelatedClasses(Parent) should return a list of type objects ([Child] in this instance). Any ideas how to do that? Thanks!


property.mapper is the target Mapper and mapper.class_ is the class which is mapped.

in 0.7 you want to use class_mapper(cls).iterate_properties() to get at MapperProperty, don't bother with __dict__ (and even then I'd be using dir()/getattr() to deal with descriptors).

There's an improved introspection interface in 0.8, see: http://docs.sqlalchemy.org/en/rel_0_8/core/inspection.html , you'd use inspect(cls).relationships.

AlexVhr

unread,
Dec 12, 2012, 11:27:50 AM12/12/12
to sqlal...@googlegroups.com
Thanks a lot, it works perfectly. On a matter of using 0.8 - is it allready stable enough to switch to? (My projects are nothing critical, but still...)

Michael Bayer

unread,
Dec 12, 2012, 12:10:49 PM12/12/12
to sqlal...@googlegroups.com
yeah 0.8 is fine, was almost ready to put 0.8.0b2 and then final the other day, just little things coming in....

--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To view this discussion on the web visit https://groups.google.com/d/msg/sqlalchemy/-/L_y3F-9AOWAJ.

AlexVhr

unread,
Dec 12, 2012, 12:38:36 PM12/12/12
to sqlal...@googlegroups.com
Glad to hear it, I'll give it a try.
Reply all
Reply to author
Forward
0 new messages