Skip to content

Instantly share code, notes, and snippets.

@tonio
Created March 28, 2012 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tonio/2225046 to your computer and use it in GitHub Desktop.
Save tonio/2225046 to your computer and use it in GitHub Desktop.
Sqlalchemy association_proxy error
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