Thanks. I wrote the error message from memory, the exact wording is:

session.query(Document).filter(Document.tags.in_(tag_list))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/site-packages/sqlalchemy/sql/
expression.py", line 1274, in in_
    return self.operate(operators.in_op, other)
  File "/usr/lib/python2.6/site-packages/sqlalchemy/orm/
attributes.py", line 120, in operate
    return op(self.comparator, *other, **kwargs)
  File "/usr/lib/python2.6/site-packages/sqlalchemy/sql/operators.py",
line 49, in in_op
    return a.in_(b)
  File "/usr/lib/python2.6/site-packages/sqlalchemy/orm/
properties.py", line 479, in in_
    raise NotImplementedError("in_() not yet supported for relations.
For a "
NotImplementedError: in_() not yet supported for relations.  For a
simple many-to-one, use in_() against the set of foreign key values.

Thanks for the suggestions.



On Mar 15, 10:54 am, "Michael Bayer" <[email protected]> wrote:
> Stodge wrote:
> > I have two classes with a third table:
>
> > document_tags = Table('document_tags', metadata,
> >    Column('document_id', Integer, ForeignKey('documents.id')),
> >    Column('tag_id', Integer, ForeignKey('tags.id'))
> > )
>
> > class Document(Base):
> >    __tablename__ = 'documents'
>
> >    id = Column(Integer, primary_key=True)
> >    title = Column(String)
> >    filename = Column(String)
> >    tags = relation('Tag', secondary=document_tags, backref='tags')
>
> >    def __init__(self, title, filename):
> >            self.title = title
> >            self.filename = filename
>
> > class Tag(Base):
> >    __tablename__ = 'tags'
>
> >    id = Column(Integer, primary_key=True)
> >    tag = Column(String)
>
> >    def __init__(self, tag):
> >            self.tag = tag
>
> > I want to find all documents with tags in a given list of tags:
>
> > documents =
> > session.query(Document).filter(Document.tags.in_(tag_list))
>
> > except I get the familiar message that the "in_()" operator is not
> > currently implemented for many-to-one-relations.
>
> > I've searched and found some alternatives but I can't get any to work.
> > Is there an easy example that will make this work? Thanks
>
> if the error message says "many-to-one" then that's a bug.  Your relation
> is many-to-many.
>
> in this case the syntactically easiest method is to use any().
> Document.tags.any(Tag.id.in_([t.id for t in tag_list])).
>
> A join could be more performant, which would be:
>
> query.join(Document.tags).filter(Tag.id.in_([t.id for t in tag_list]))
>
>
>
> > --
> > 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.

Reply via email to