<p dir="auto"><strong>Migrated issue, originally created by Fayaz Khan (<a href="https://github.com/fayaz">@fayaz</a>)</strong></p>
<p dir="auto">In this specific scenario, <code class="notranslate">query.count()</code> &amp; <code class="notranslate">len(query.all())</code> do not match:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="from sqlalchemy import create_engine, Column, ForeignKey, Integer, Table
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, sessionmaker


Base = declarative_base()
promo_like_association = Table(
    'promo_like_association', Base.metadata,
    Column('promo_id', ForeignKey('promo.id'), primary_key=True),
    Column('like_id', ForeignKey('like.id'), primary_key=True))
user_like_association = Table(
    'user_like_association', Base.metadata,
    Column('user_id', ForeignKey('user.id'), primary_key=True),
    Column('like_id', ForeignKey('like.id'), primary_key=True))


class Like(Base):
    __tablename__ = 'like'
    id = Column(Integer, primary_key=True)
    users = relationship(
        'User', secondary=user_like_association, backref='likes')


class Promo(Base):
    __tablename__ = 'promo'
    id = Column(Integer, primary_key=True)
    likes = relationship(Like, secondary=promo_like_association)


class User(Base):
    __tablename__ = 'user'
    id = Column(Integer, primary_key=True)


def main():
    database_uri = 'sqlite:///'
    engine = create_engine(database_uri)
    Session = sessionmaker(bind=engine)
    session = Session()
    Base.metadata.create_all(engine)
    user = User(likes=[Like()])
    session.add(user)
    session.add(Promo())
    check(session, Like.users.any(id=user.id))
    check(session, Like.users.contains(user))
    user.likes.append(Like())
    check(session, Like.users.any(id=user.id))
    check(session, Like.users.contains(user))  # Fails


def check(session, condition):
    # query tries to fetch promos for user with common likes
    # or promos that have no specific likes at all.
    query = (session.query(Promo).outerjoin(Promo.likes)
             .filter(condition | ~Promo.likes.any()))
    assert query.count() == len(query.all()) == 1


if __name__ == '__main__':
    main()
"><pre class="notranslate"><code class="notranslate">from sqlalchemy import create_engine, Column, ForeignKey, Integer, Table
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, sessionmaker


Base = declarative_base()
promo_like_association = Table(
    'promo_like_association', Base.metadata,
    Column('promo_id', ForeignKey('promo.id'), primary_key=True),
    Column('like_id', ForeignKey('like.id'), primary_key=True))
user_like_association = Table(
    'user_like_association', Base.metadata,
    Column('user_id', ForeignKey('user.id'), primary_key=True),
    Column('like_id', ForeignKey('like.id'), primary_key=True))


class Like(Base):
    __tablename__ = 'like'
    id = Column(Integer, primary_key=True)
    users = relationship(
        'User', secondary=user_like_association, backref='likes')


class Promo(Base):
    __tablename__ = 'promo'
    id = Column(Integer, primary_key=True)
    likes = relationship(Like, secondary=promo_like_association)


class User(Base):
    __tablename__ = 'user'
    id = Column(Integer, primary_key=True)


def main():
    database_uri = 'sqlite:///'
    engine = create_engine(database_uri)
    Session = sessionmaker(bind=engine)
    session = Session()
    Base.metadata.create_all(engine)
    user = User(likes=[Like()])
    session.add(user)
    session.add(Promo())
    check(session, Like.users.any(id=user.id))
    check(session, Like.users.contains(user))
    user.likes.append(Like())
    check(session, Like.users.any(id=user.id))
    check(session, Like.users.contains(user))  # Fails


def check(session, condition):
    # query tries to fetch promos for user with common likes
    # or promos that have no specific likes at all.
    query = (session.query(Promo).outerjoin(Promo.likes)
             .filter(condition | ~Promo.likes.any()))
    assert query.count() == len(query.all()) == 1


if __name__ == '__main__':
    main()

</code></pre></div>
<p dir="auto">This will fail with:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Traceback (most recent call last):
  File &quot;test_sqla_contains.py&quot;, line 60, in &lt;module&gt;
    main()
  File &quot;test_sqla_contains.py&quot;, line 48, in main
    check(session, Like.users.contains(user))  # Fails
  File &quot;test_sqla_contains.py&quot;, line 56, in check
    assert query.count() == len(query.all()) == 1
AssertionError
"><pre class="notranslate"><code class="notranslate">Traceback (most recent call last):
  File "test_sqla_contains.py", line 60, in &lt;module&gt;
    main()
  File "test_sqla_contains.py", line 48, in main
    check(session, Like.users.contains(user))  # Fails
  File "test_sqla_contains.py", line 56, in check
    assert query.count() == len(query.all()) == 1
AssertionError

</code></pre></div>
<p dir="auto">I'm working around the issue using <code class="notranslate">any(id=...)</code> instead of <code class="notranslate">contains()</code>.</p>
<p dir="auto"><strong>Migrated issue, originally created by Anonymous</strong></p>
<p dir="auto">Currently it throws error at</p>
<p dir="auto">File "C:\Python26\lib\site-packages\sqlalchemy-0.7.6-py2.6-win32.egg\sqlalchemy\sql\compiler.py", line 927, in visit_table<br>
hinttext = self.get_from_hint_text(table, fromhints<a href="table">table</a>)<br>
TypeError: get_from_hint_text() takes exactly 2 arguments (3 given)</p>
<p dir="auto">the problem is that signature in mssql dialect is</p>
<p dir="auto">def get_from_hint_text(self, text):<br>
return text</p>
<p dir="auto">Hence we should change the either the signature by adding table as argument or remove table parameter from sqlalchemy\sql\compiler.py", line 927</p>