[Sqlalchemy-tickets] [sqlalchemy] #2330: with_only_columns([func.count('*')]) works wrong in sqalch
Brought to you by:
zzzeek
From: sqlalchemy <mi...@zz...> - 2011-11-17 20:01:19
|
#2330: with_only_columns([func.count('*')]) works wrong in sqalchemy 0.7.3 --------------------+------------------------------------------------------- Reporter: guest | Owner: zzzeek Type: defect | Status: new Priority: medium | Milestone: Component: sql | Severity: no triage selected yet Keywords: | Status_field: awaiting triage --------------------+------------------------------------------------------- query.with_only_columns([func.count('*')]) seems to produce invalid SQL since slqalchemy 0.7.3 (tested on fresh clone from hg too) code to reproduce it(tested also on mysql - the same issue): {{{ from sqlalchemy import ForeignKey, create_engine, select, func from sqlalchemy.orm import sessionmaker, relationship from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Column, Integer, DateTime, Unicode Base = declarative_base() engine = create_engine('sqlite://', echo=False) Session = sessionmaker(autoflush=False, bind=engine) class Foo(Base): __tablename__ = 'foos' id = Column(Integer(), primary_key=True) xxx = Column(Unicode(10), nullable=False, default=u'xxx') session = Session() Base.metadata.create_all(engine) session.add_all(Foo() for _ in xrange(30)) session.commit() foos_table = Foo.__table__ query = select([foos_table]) # doesn't work:/ (query: SELECT count(?) AS count_1) count = query.with_only_columns([func.count('*')]) # wrong result: 1 print '\tcount *: %i' % session.execute(count).scalar() # work (query: SELECT count(foos.id) AS count_1 FROM foos) count = query.with_only_columns([func.count(foos_table.c.id)]) # correct result: 30 print '\tcount id: %i' % session.execute(count).scalar() }}} test runs: sqlalchemy 0.7.3 {{{ $ python bug_with_only_columns.py count *: 1 count id: 30 }}} sqlalchemy 0.7.2 {{{ $ python bug_with_only_columns.py count *: 30 count id: 30 }}} -- Ticket URL: <http://www.sqlalchemy.org/trac/ticket/2330> sqlalchemy <http://www.sqlalchemy.org/> The Database Toolkit for Python |