[Grok-dev] Modeling a class that extend from list() with megrok.rdb

Hector Blanco white.lists at gmail.com
Fri Oct 29 10:48:02 EDT 2010


Hello list...

I thing I almost have it, but I am still getting problems mapping an
structure like this.

class Child(rdb.Model):
       def __init__(self):
               self.field1 = “hello world”

class Parent(rdb.Model):
       def __init__(self):
               self.child1 = Child()
               self.child2 = Child()

(I have modified it from the former question because maybe it'll be
easy to understand)

The “Parent” class has two different instances of a Child() class. I
am not even sure about how to treat this (two different 1:1
relationships or a 1:2 relationship).

My last try (out of many) is this mapping:

class Child(rdb.Model):
       rdb.metadata(metadata)
       rdb.tablename("children_table")
       id = Column("id", Integer, primary_key=True)
       field1 = Column(“field1”, String(64))   #Irrelevant
       def __init__(self):
               self.field1 = “hello world”

class Parent(rdb.Model):
       rdb.metadata(metadata)
       rdb.tablename("parent_table”)

       id = Column("id", Integer, primary_key=True)
       child1_id = Column("child_1_id", Integer,
ForeignKey("children_table.id"))
       child2_id = Column("child_2_id", Integer,
ForeignKey("children_table.id"))

       child1 = relationship(Child,
               primaryjoin = (child1_id=="children_table.id")
       )

       child2 = relationship(Child,
               primaryjoin = (child2_id=="children_table.id")
       )

I have tried (almost) everything. I say “almost” because obviously I
haven't tried the right thing. I keep getting errors that sqlalchemy
can't determine the relationship between the two tables.

It looks very similar to:
http://www.sqlalchemy.org/docs/orm/relationships.html#multiple-relationships-against-the-same-parent-child

but I also understand that maybe I can't get this working that way,
because I am actually putting two ForeignKeys from the same table
(“children_table”) in the parent table. I am not sure how that will
work out, or is correct or what... :-(

Thank you!


More information about the Grok-dev mailing list