from sqlalchemy import Unicode, Integer, Column, create_engine,
ForeignKey
from sqlalchemy.orm import relationship, Session
from sqlalchemy.orm.collections import MappedCollection
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.associationproxy import association_proxy
import operator
class Base(object):
id = Column(Integer, primary_key=True)
Base = declarative_base(cls=Base)
def _create_c_by_value(value):
return C(value)
def _create_a_by_value(value):
return A(value)
class A(Base):
__tablename__ = "a"
id = Column(Integer, primary_key=True)
value = Column(Unicode)
associations = relationship("B", cascade="all")
c_values = association_proxy("associations", "c_val",
creator=_create_c_by_value)
def __init__(self, val):
self.value = val
def __repr__(self):
return('<A>(%s)' % self.value)
class B(Base):
__tablename__ = "b"
a_id = Column(Integer, ForeignKey("a.id"), nullable=False)
c_id = Column(Integer, ForeignKey("c.id"), nullable=False)
c_elements = relationship("C", cascade="all")
c_val = association_proxy("c_elements", "value")
a_elements = relationship("A", cascade="all")
a_val = association_proxy("a_elements", "value")
class C(Base):
__tablename__ = "c"
id = Column(Integer, primary_key=True)
c_value = Column(Unicode)
associations = relationship("B", cascade="all")
a_values = association_proxy("associations", "a_val",
creator=_create_a_by_value)
def __init__(self, val):
self.value = val
def __repr__(self):
return('<C>(%s)' % self.value)
if __name__ == "__main__":
engine = create_engine('sqlite://', echo=True)
Base.metadata.create_all(engine)
session = Session(engine)
Ok, running this sets up an example environment then you can
experiment. Try creating some As and relating them to some Cs. I'm
getting a bunch of different errors doing this. What am I doing wrong
or overlooking?
The core of my inquiry is this: would this be the way to make two-way
relationships between two classes (two classes which have a many:many
relationship between them) work correctly when tunneled through an
associationproxy to hide the association object class between them?
--
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.