Created
March 28, 2012 09:32
-
-
Save tonio/2225046 to your computer and use it in GitHub Desktop.
Sqlalchemy association_proxy error
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sqlalchemy.ext.declarative import declarative_base | |
from sqlalchemy import * | |
from sqlalchemy.orm import sessionmaker, relationship | |
from sqlalchemy.ext.associationproxy import association_proxy | |
Base = declarative_base() | |
class A(Base): | |
__tablename__ = 'a' | |
id = Column(Integer, primary_key=True) | |
class B(Base): | |
__tablename__ = 'b' | |
id = Column(Integer, primary_key=True) | |
id_a = Column(Integer, ForeignKey('a.id')) | |
a = relationship('A', backref='bs') | |
class C(Base): | |
__tablename__ = 'c' | |
id = Column(Integer, primary_key=True) | |
id_b = Column(Integer, ForeignKey('b.id')) | |
b = relationship('B', backref='cs') | |
a = association_proxy('b', 'a') | |
engine = create_engine('sqlite://', echo=True) | |
Base.metadata.create_all(engine) | |
sess = sessionmaker(engine)() | |
a1 = A() | |
b1 = B() | |
c1 = C() | |
c2 = C() | |
a1.bs.append(b1) | |
b1.cs.append(c1) | |
sess.add_all([ | |
a1, b1, c1, c2 | |
]) | |
# querying works | |
print sess.query(C).filter(C.a==a1) | |
# access works | |
print c1.a | |
# AttributeError: 'NoneType' object has no attribute 'a' | |
print c2.a |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment