Yeah, changing the order actually works out just fine...I can even do:
mc1_1 = MyClass1()
mc2_1 = MyClass2()
mc2_2 = MyClass2()
mc3_1 = MyClass3()
mc3_2 = MyClass3()
mc1_1.myclasses.append(mc2_1)
mc2_2.parent=mc1_1
mc2_1.myclasses.append(mc3_1)
mc3_2.parent = mc2_1
for x in mc1_1.myclasses:
print x
for y in x.myclasses:
print y
And it will output the classes just as expected...I´ll take this to a
more complex practical case, lets see what happens...
By the way, thanks for the help!!
Gabriel.
Qvx escribió:
I'm not sure. It must be some issue with ActiveMapper. For
example, this works (note the changed order of classes):
----------------
from sqlalchemy.ext.activemapper import ActiveMapper, column,
one_to_many, one_to_one
from sqlalchemy import create_engine, objectstore, and_, or_,
ForeignKey, String, Integer, DateTime, Binary, Boolean, Date, backref
import sqlalchemy.ext.activemapper as activemapper
__engine__ = create_engine('sqlite://filename=:memory:', echo=True)
class MyClass1(ActiveMapper):
class mapping:
__table__ = "my_tab1"
id = column(Integer, primary_key=True)
myclasses = one_to_many('MyClass2', backref='parent')
class MyClass3(ActiveMapper):
class mapping:
__table__ = "my_tab3"
id = column(Integer, primary_key=True)
parent_id = column(Integer, foreign_key='my_tab2.id')
class MyClass2(ActiveMapper):
class mapping:
__table__ = "my_tab2"
id = column(Integer, primary_key=True)
parent_id = column(Integer, foreign_key='my_tab1.id')
myclasses = one_to_many('MyClass3', backref='parent')
activemapper.create_tables()
mc1_1 = MyClass1()
mc2_1 = MyClass2()
mc2_2 = MyClass2()
mc1_1.myclasses.append(mc2_1)
mc2_2.parent = mc1_1
for x in mc1_1.myclasses:
print x
----------
I usually create classes using ActiveMapper and add whatever properties
I need. Later on, separately from Class definition, I add complex
relations using add_property. Like this:
class CCKampanja(ActiveMapper):
class mapping:
__table__ = 'cc_kampanja'
id = column(Integer, primary_key=True,
default=func.cc_kampanja_seq.nextval())
naziv = column(Unicode(100))
opis = column(Unicode(255))
velicina_populacije = column(Integer)
proizvod = column(Unicode(100))
datum_definiranja = column(DateTime)
datum_pocetka = column(DateTime)
datum_zavrsetka = column(DateTime)
vrsta_izvora = column(Unicode(10))
izvor = column(Unicode(250))
sugarcrm_campaign_id = column(Unicode(36))
stranke = one_to_many('CCStranka',
backref='kampanja', lazy=True)
@propgetmemo
def broj_odabranih_jmbg(self):
return select([func.count(CCStranka.c.id)],
and_(CCStranka.c.kampanja_id==self.id,
CCStranka.c.odabrano=='D',
not_(CCStranka.c.stranka==None))).scalar()
@propgetmemo
def job_us(self):
return joblib.get_job(job_type='CC-UVOZ-STRANKA',
source_type='CC_KAMPANJA', source_ref=
self.id)
CCStranka.mapper.add_property(
'iz_imenika', relation(CCStrankaImenik.mapper,
order_by=desc(CCStrankaImenik.c.tezina), lazy=False))
...
CCUvoz.mapper.add_property(
'kolone', relation(CCKolonaUvoza.mapper,
order_by = CCKolonaUvoza.c.pozicija,
private = True,
backref = 'uvoz'))
On 4/4/06,
Gabriel Jacobo <[EMAIL PROTECTED]>
wrote:
I´m sorry but that does not seem to
work either...I will paste the
exact example I´m testing, and maybe you can try it out. This is using
python 2.4 and sqlalchemy 0.15 under windows, by the way...
test.py
---
from sqlalchemy.ext.activemapper import ActiveMapper, column,
one_to_many, one_to_one
from sqlalchemy import objectstore, and_, or_, ForeignKey, String,
Integer, DateTime, Binary, Boolean, Date, backref
import sqlalchemy.ext.activemapper as activemapper
class MyClass1(ActiveMapper):
class mapping:
__table__ = "my_tab1"
id = column(Integer, primary_key=True)
myclasses = one_to_many('MyClass2', backref='parent')
class MyClass2(ActiveMapper):
class mapping:
__table__ = "my_tab2"
id = column(Integer, primary_key=True)
parent_id = column(Integer, foreign_key='my_tab1.id')
myclasses =
one_to_many('MyClass3', backref='parent') #If you
comment out this line, everything will work just fine...
class MyClass3(ActiveMapper):
class mapping:
__table__ = "my_tab3"
id = column(Integer, primary_key=True)
parent_id = column(Integer, foreign_key='my_tab2.id')
activemapper.engine.connect('sqlite://filename=:memory:')
activemapper.create_tables()
mc1_1 = MyClass1()
mc2_1 = MyClass2()
mc2_2 = MyClass2()
mc1_1.myclasses.append(mc2_1)
mc2_2.parent=mc1_1
for x in mc1_1.myclasses:
print x
---
With the marked line commented out you get the two classes printed at
the end, so everything seems in order. But with the "one_to_many"
relationship enabled in MyClass2 you get an exception, KeyError,
because it does not find the 'parent' key in MyClass2 when
"mc1_1.myclasses.append(mc2_1)" gets executed.
Thanks for your help.
Gabriel.
Qvx escribió:
I have it like this and it works:
class MyClass1(ActiveMapper):
class mapping:
__table__ = "my_tab1"
id = column(Integer, primary_key=True)
myclasses = one_to_many('MyClass2', backref='parent')
class MyClass2(ActiveMapper):
class mapping:
__table__ = "my_tab2"
id = column(Integer, primary_key=True)
parent_id = column(Integer, foreign_key='my_tab1.id')
myclasses = one_to_many('MyClass3', backref='parent')
class MyClass3(ActiveMapper):
class mapping:
__table__ = "my_tab3"
id = column(Integer, primary_key=True)
parent_id = column(Integer, foreign_key='my_tab2.id')
On 4/4/06, Gabriel
Jacobo <[EMAIL PROTECTED]>
wrote:
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
|