*Hi M. Bayer and thanks for your answer,
i m doing exactly what explain here :
http://docs.sqlalchemy.org/en/latest/orm/extensions/associationproxy.html#simplifying-association-objects
it works well
but when i want to append a new element in relationship i have an error due
to creator function missing*
*here part of my model*
# Many to Many
class DossierTarife(Base):
__tablename__ = 'tarifer_dossier'
IdDossier = Column(Integer, ForeignKey('dossier.IdDossier'),
primary_key=True)
IdAt = Column(Integer, ForeignKey('article_tarife.IdAt'),
primary_key=True)
dossier = relationship(Dossier, backref=backref("tarifer_dossier",
cascade="all, delete-orphan"))
article_tarife = relationship(ArticleTarife,
backref=backref("tarifer_dossier"))
class Dossier(Base):
__tablename__ = 'dossier'
IdDossier = Column('IdDossier', Integer, primary_key=True)
...
class ArticleTarife(Base):
__tablename__ = 'article_tarife'
IdAt = Column('IdAt', Integer, primary_key=True)
...
Dossier.LesTar = association_proxy("tarifer_dossier", "article_tarife")
ArticleTarife.LesTar = association_proxy("tarifer_dossier", "dossier")
*I do my simulation with this*
print "====================================="
d =
model.session.query(model.Dossier).filter(model.Dossier.IdDossier==500315).first()
print d.NomDossier, len(d.LesTar), d.LesTar
print "------"
s =
model.session.query(model.ArticleTarife).filter(model.ArticleTarife.IdAt==366).first()
if s in d.LesTar:
d.LesTar.remove(s)
else:
d.LesTar.append(s)
print len(d.LesTar), d.LesTar
common.merge_object(d)
print s.Nom or s.Valeur
print s.LesTar
print "-------------------"
print "========================================"
*It works well, i can access articletarife objects from dossier and dossier
objects from articletarife*
=====================================
VIANEOS 9 [<model.ArticleTarife object at 0x06E4B0F0>, <model.ArticleTarife
object at 0x06E4BE30>, <
model.ArticleTarife object at 0x06E4B190>, <model.ArticleTarife object at
0x06E4BEB0>, <model.Articl
eTarife object at 0x06E4B8D0>, <model.ArticleTarife object at 0x06E3F310>,
<model.ArticleTarife obje
ct at 0x06E4B210>, <model.ArticleTarife object at 0x06E4BFB0>,
<model.ArticleTarife object at 0x06E2
8F90>]
------
8 [<model.ArticleTarife object at 0x06E4BE30>, <model.ArticleTarife object
at 0x06E4B190>, <model.Ar
ticleTarife object at 0x06E4BEB0>, <model.ArticleTarife object at
0x06E4B8D0>, <model.ArticleTarife
object at 0x06E3F310>, <model.ArticleTarife object at 0x06E4B210>,
<model.ArticleTarife object at 0x
06E4BFB0>, <model.ArticleTarife object at 0x06E28F90>]
1.13
[<model.Dossier object at 0x06C69490>, <model.Dossier object at
0x06C69C10>, <model.Dossier object a
t 0x06C69270>, <model.Dossier object at 0x06381250>, None, <model.Dossier
object at 0x06381930>, <mo
del.Dossier object at 0x06381D50>, <model.Dossier object at 0x06381CF0>,
<model.Dossier object at 0x
063A3330>, None, None, None, None, None, <model.Dossier object at
0x06E28CF0>, <model.Dossier object
*But when sqlalchemy need to append a new element to relation ship, i have
this error*
Traceback (most recent call last):
File "atao.py", line 2, in <module>
main.run()
File "I:\main.py", line 392, in run
app = AtaoApplication(redirect=False)
File "C:\Python26\lib\site-packages\wx-2.8-msw-unicode\wx\_core.py", line
7981, in __init__
self._BootstrapApp()
File "C:\Python26\lib\site-packages\wx-2.8-msw-unicode\wx\_core.py", line
7555, in _BootstrapApp
return _core_.PyApp__BootstrapApp(*args, **kwargs)
File "I:\main.py", line 383, in OnInit
param.main_window = MainWindow()
File "I:\main.py", line 172, in __init__
d.LesTar.append(s)
File
"C:\Python26\lib\site-packages\sqlalchemy-0.7.2-py2.6-win32.egg\sqlalchemy\ext\associationpro
xy.py", line 469, in append
item = self._create(value)
File
"C:\Python26\lib\site-packages\sqlalchemy-0.7.2-py2.6-win32.egg\sqlalchemy\ext\associationpro
xy.py", line 396, in _create
return self.creator(value)
TypeError: __init__() takes exactly 1 argument (2 given)
*
i read what explain here
http://docs.sqlalchemy.org/en/latest/orm/extensions/associationproxy.html#creation-of-new-values
**but i have no intermediary object, i have the full object like classic
many to many relationship pattern describe here :
http://docs.sqlalchemy.org/en/latest/orm/tutorial.html#building-a-many-to-many-relationship
.
What do i need in my model to have the same behaviour that classic many to
many relationship ?
I already try adding : creator=lambda x: x to my association proxies*
2012/3/27 Michael Bayer <[email protected]>
>
> On Mar 27, 2012, at 4:35 AM, Christian Démolis wrote:
>
> Up!
>
> 2012/3/23 Christian Démolis <[email protected]>
>
>> Hi all,
>>
>> class A(Base):
>> __tablename__ = 'a'
>> IdA = Column('IdA', Integer, primary_key=True)
>> AllTheB = association_proxy("many_to_many_relation", "relation_b")
>>
>> class ManyToManyRelation(
>> __tablename__ = 'many_to_many_relation'
>> IdA = Column(Integer, ForeignKey('A.IdA'), primary_key=True)
>> IdB = Column(Integer, ForeignKey('B.IdB'), primary_key=True)
>>
>> relation_a = relationship(A, backref=backref("tarifer_dossier",
>> cascade="all, delete-orphan"))
>> relation_b = relationship(B, backref=backref("tarifer_dossier",
>> cascade="all, delete-orphan"))
>>
>> class B(Base):
>> __tablename__ = 'b'
>> IdB = Column('IdB ', Integer, primary_key=True)
>> AllTheA = association_proxy("many_to_many_relation", "relation_a")
>>
>> x_a is instance of A
>> x_b is instance of B
>>
>> x_a.AllTheB returns me all the objects B relative to x_a
>>
>> When i want to append new element e (instance of A)
>> x_a.AllTheB.append(e)
>>
>> I have an error due to create mechanism of association_prox
>>
>> How can i have the same (simple) behaviour of classical many to many
>> relationship ?
>>
>> Thanks in advance
>>
>> Chris
>>
>
>
> Did you try doing exactly what's described at
> http://docs.sqlalchemy.org/en/latest/orm/extensions/associationproxy.html#simplifying-association-objects?
> the example above is not connecting things together correctly; A and
> B's association proxies would need to refer to "tarifer_dossier", that is,
> the direction from A->M2M and B->M2M. Each association proxy then has a
> "creator" which is a lambda that creates ManyToManyRelation in exactly the
> way it should based on the given input (see
> http://docs.sqlalchemy.org/en/latest/orm/extensions/associationproxy.html#creation-of-new-values
> ).
>
>
>
> --
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To post to this group, send email to [email protected].
> 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.
>
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected].
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.