Skip to content

Query.exists() don't works with queries without filters #2818

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
sqlalchemy-bot opened this issue Sep 3, 2013 · 10 comments
Closed

Query.exists() don't works with queries without filters #2818

sqlalchemy-bot opened this issue Sep 3, 2013 · 10 comments
Labels
bug Something isn't working high priority orm
Milestone

Comments

@sqlalchemy-bot
Copy link
Collaborator

Migrated issue, originally created by Vladimir Magamedov (@vmagamedov)

Action:

db.session.query(User).exists()

Expectation:

SELECT 1 FROM users

Current result:

SELECT 1

Solution:

diff --git a/lib/sqlalchemy/orm/query.py b/lib/sqlalchemy/orm/query.py
index b71bfe0..d64575a 100644
--- a/lib/sqlalchemy/orm/query.py
+++ b/lib/sqlalchemy/orm/query.py
@@ -2494,7 +2494,7 @@ class Query(object):
         .. versionadded:: 0.8.1

         """
-        return sql.exists(self.with_entities('1').statement)
+        return sql.exists(self.statement.with_only_columns(['1']('1')))

     def count(self):
         """Return a count of rows this Query would return.
diff --git a/test/orm/test_query.py b/test/orm/test_query.py
index 0973dc3..bc56668 100644
--- a/test/orm/test_query.py
+++ b/test/orm/test_query.py
@@ -1730,9 +1730,16 @@ class ExistsTest(QueryTest, AssertsCompiledSQL):
     def test_exists(self):
         User = self.classes.User
         sess = create_session()
-        q1 = sess.query(User).filter(User.name == 'fred')
+        q1 = sess.query(User)
         self.assert_compile(sess.query(q1.exists()),
             'SELECT EXISTS ('
+                'SELECT 1 FROM users'
+            ') AS anon_1',
+            dialect=default.DefaultDialect()
+        )
+        q2 = sess.query(User).filter(User.name == 'fred')
+        self.assert_compile(sess.query(q2.exists()),
+            'SELECT EXISTS ('
                 'SELECT 1 FROM users WHERE users.name = :name_1'
             ') AS anon_1',
             dialect=default.DefaultDialect()

P.S. Is it normal to send pull requests on GitHub? Or you prefer Trac/patches?

@sqlalchemy-bot
Copy link
Collaborator Author

Michael Bayer (@zzzeek) wrote:

ah that's a very nice approach, and test, great. sure, send a pullreq via github or bitbucket and I'll fast-track it (i have a some pullreqs backed up at the moment).

@sqlalchemy-bot
Copy link
Collaborator Author

Changes by Michael Bayer (@zzzeek):

  • added labels: high priority

@sqlalchemy-bot
Copy link
Collaborator Author

Michael Bayer (@zzzeek) wrote:

e816754 master

5d1bd98 0.8

@sqlalchemy-bot
Copy link
Collaborator Author

Changes by Michael Bayer (@zzzeek):

  • changed status to closed

@sqlalchemy-bot
Copy link
Collaborator Author

Michael Bayer (@zzzeek) wrote:

bit of a glitch:

import sqlalchemy as sa
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class ModelBase(Base):
    __tablename__ = 'ModelBases'
    id = sa.Column(sa.Integer, primary_key=True)
    model_type = sa.Column(sa.Integer)
    __mapper_args__ = {'polymorphic_on': model_type}

class Model(ModelBase):
    __tablename__ = 'Models'
    __mapper_args__ = {'polymorphic_identity': 1}
    id = sa.Column(sa.ForeignKey(ModelBase.id), primary_key=True)

sess = sa.orm.Session()

print sess.query(Model).exists()

generates a select columns warning because use_labels isn't set.

@sqlalchemy-bot
Copy link
Collaborator Author

Changes by Michael Bayer (@zzzeek):

  • changed status to reopened

@sqlalchemy-bot
Copy link
Collaborator Author

Michael Bayer (@zzzeek) wrote:

eee219b master /0.9

2576b5c 0.8

@sqlalchemy-bot
Copy link
Collaborator Author

Changes by Michael Bayer (@zzzeek):

  • changed status to closed

@sqlalchemy-bot
Copy link
Collaborator Author

Michael Bayer (@zzzeek) wrote:

39a8e2e

@sqlalchemy-bot
Copy link
Collaborator Author

Michael Bayer (@zzzeek) wrote:

d8aa3d9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working high priority orm
Projects
None yet
Development

No branches or pull requests

1 participant