Hi! I´ve been trying SQLAlchemy using Activemapper for a couple of days
and I´ve found an issue that I did not see reflected anywhere else, so
please excuse if this is a silly question.
The thing is I have a schema that´s similar to the one stated at the end
of the message. Basically it consists of three tables, each of which is
related in a kind of one parent- multiple child relation to each other,
that is...MyClass1 "owns" multiple MyClass2, and MyClass2 in turn "owns"
multiple MyClass3. Each class, where apropriate, keeps a list of their
children and receives a backref to their parent.
I don´t know if there is a fundamental problem in this way of doing
things, but the point is this just doesn´t work. And the problem seems
to be in the backrefs. For example, if I do the following:
m1 = MyClass1()
m2 = MyClass2()
m1.myclasses.append(m2)
The code will fail, saying that MyClass2 does not have a "parent"
attribute. I believe this happens because the code for ActiveMapper
processes relations as follows:
1) It adds a mapper for MyClass1 to handle its relations. Seeing that
MyClass1 has a backref, it modifies MyClass2 to add the backref.
2) It adds a mapper for MyClass2 to handle its relations, in this case
with MyClass3. But in doing this, the process erases the backref
(represented by the "parent" attribute) to MyClass1! I´ve tested this by
painfully executing step by step until I found a point where this can be
easily seen (I believe in the assign_mapper function that gets called by
ActiveMapper).
I´ve tried replacing the backrefs by what is called "Circular Mapping"
in the docs, using AddProperty to the mapper, but it does not seem to
work, probably because I am not doing it right. My final question would
then be if there is some way to accomplish what I´m trying to do with
ActiveMapper, or with the "traditional" method for using SQLAlchemy. If
this solution should involve modifying ActiveMapper I would be glad to
do it as it provides a much clearer schema than using the traditional
method.
Of course, if I eliminate the backref in MyClass1 or the one-to-many
relation in Class2, everything works fine, but this is not what I intend
to do.
Thanks for any help you can give me!
Gabriel.
PD: The schema...
class MyClass1(Activemapper):
class mapping:
__table__ = "MyClass1"
id = column(Integer, primary_key=True)
myclasses = one_to_many('MyClass2', colname='parent_id',
backref='parent')
class MyClass2(Activemapper):
class mapping:
__table__ = "MyClass1"
id = column(Integer, primary_key=True)
parent_id = column(Integer, foreign_key=ForeignKey('MyClass1.id'))
myclasses = one_to_many('MyClass3', colname='parent_id',
backref='parent')
class MyClass3(Activemapper):
class mapping:
__table__ = "MyClass3"
id = column(Integer, primary_key=True)
parent_id = column(Integer, foreign_key=ForeignKey('MyClass2.id'))
-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
Sqlalchemy-users mailing list
Sqlalchemy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users