Hi,
I was trying to work through the many-to-many relationship tutorial and
understand the concept of Association Proxies. (Using SA-0.8.2)
Here's my model
book_author_table = Table('book_author',Base.metadata,
Column('book_id',Integer,ForeignKey('book.id')),
Column('author_id',Integer,ForeignKey('author.id'))
)
class Book(Base):
__tablename__ = 'book'
__table_args__ = {'mysql_engine':'InnoDB'}
id = Column(Integer,primary_key=True,autoincrement=True)
name = Column(String(20))
authors =
relationship("Author",backref="books",secondary=book_author_table)
aths = association_proxy('authors','name')
def __init__(self,name):
self.name = name
class Author(Base):
__tablename__ = 'author'
__table_args__ = {'mysql_engine':'InnoDB'}
id = Column(Integer,primary_key=True,autoincrement=True)
name = Column(String(20))
def __init__(self,name):
self.name = name
Populating these
b1=model.Book('B1')
b2=model.Book('B2')
session.add(b1)
session.add(b2)
a1=model.Author('A1')
a2=model.Author('A2')
a3=model.Author('A3')
a4=model.Author('A4')
session.add_all([a1,a2,a3,a4])
b1.authors.append(a1)
b1.authors.append(a2)
b2.authors.extend([a1,a3,a4,])
Statements like these work
>>>
session.query(model.Book).filter(model.Book.authors.any(name='A1')).all()
['B1','B2',]
>From the docs
(http://docs.sqlalchemy.org/en/rel_0_8/orm/extensions/associationproxy.html#querying-with-association-proxies),
this should also work
>>> session.query(model.Book).filter(model.Book.aths.any(name='A1')).all()
AttributeError: 'ColumnProperty' object has no attribute 'uselist'
Am I missing something here?
Regards,
Dheeraj
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.