<p dir="auto"><strong>Migrated issue, originally created by Anonymous</strong></p>
<p dir="auto">query.with_only_columns(<a href="func.count('*')">func.count('*')</a>) seems to produce invalid SQL since slqalchemy 0.7.3 (tested on fresh clone from hg too)</p>
<p dir="auto">code to reproduce it(tested also on mysql - the same issue):</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="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](foos_table))

# doesn't work:/ (query: SELECT count(?) AS count_1)
count = query.with_only_columns([func.count('*')](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)](func.count(foos_table.c.id)))
# correct result: 30
print '\tcount id: %i' % session.execute(count).scalar()"><pre class="notranslate"><code class="notranslate">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](foos_table))

# doesn't work:/ (query: SELECT count(?) AS count_1)
count = query.with_only_columns([func.count('*')](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)](func.count(foos_table.c.id)))
# correct result: 30
print '\tcount id: %i' % session.execute(count).scalar()
</code></pre></div>
<p dir="auto">test runs:</p>
<p dir="auto">sqlalchemy 0.7.3</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="$ python bug_with_only_columns.py 
	count *: 1
	count id: 30"><pre class="notranslate"><code class="notranslate">$ python bug_with_only_columns.py 
	count *: 1
	count id: 30
</code></pre></div>
<p dir="auto">sqlalchemy 0.7.2</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="$ python bug_with_only_columns.py 
	count *: 30
	count id: 30"><pre class="notranslate"><code class="notranslate">$ python bug_with_only_columns.py 
	count *: 30
	count id: 30
</code></pre></div>
<p dir="auto"><strong>Migrated issue, originally created by Anonymous</strong></p>
<p dir="auto">Playing with append_constraints() i noticed various errors, maybe not all grave defects, mostly bad error handling when the user does stupid things, but here they are:</p>
<ol dir="auto">
<li>
<p dir="auto">Appending a broken constraint to a table (very degenerated case...) creates a cryptic error message..., should probably tell me that my PrimaryKeyConstraint is broken...</p>
<blockquote>
<blockquote>
<blockquote>
<p dir="auto">from sqlalchemy import *<br>
meta = MetaData()<br>
users_table = Table('users',meta,<br>
...     Column('id',Integer,primary_key=True))<br>
ref_table = Table('foo',meta,Column('id_ref',Integer))<br>
pk = PrimaryKeyConstraint(<a href="'id_ref'">'id_ref'</a>,<a href="users_table.c.id">users_table.c.id</a>)<br>
ref_table.append_constraint(pk)<br>
Traceback (most recent call last):<br>
File "", line 1, in <br>
File "c:\sdk\win32\1.0\Python25\lib\site-packages\sqlalchemy-0.4.1-py2.5.egg\s<br>
qlalchemy\schema.py", line 270, in append_constraint<br>
constraint._set_parent(self)<br>
File "c:\sdk\win32\1.0\Python25\lib\site-packages\sqlalchemy-0.4.1-py2.5.egg\s<br>
qlalchemy\schema.py", line 902, in _set_parent<br>
self.append_column(table.c<a href="c">c</a>)<br>
File "c:\sdk\win32\1.0\Python25\lib\site-packages\sqlalchemy-0.4.1-py2.5.egg\s<br>
qlalchemy\util.py", line 275, in <strong>getitem</strong><br>
return self._data<a href="key">key</a><br>
TypeError: list objects are unhashable</p>
</blockquote>
</blockquote>
</blockquote>
</li>
<li>
<p dir="auto">Adding the same constraint more than once does not weed out duplicates, it just adds the ForeignKey() to the Column...</p>
<blockquote>
<blockquote>
<blockquote>
<p dir="auto">pk = ForeignKeyConstraint(<a href="'id_ref'">'id_ref'</a>,<a href="users_table.c.id">users_table.c.id</a>)<br>
ref2_table.append_constraint(pk)<br>
ref2_table<br>
Table('foo2', MetaData(None), Column('id_ref', Integer(), ForeignKey('users.id')<br>
), Column('name', String(length=30, convert_unicode=False)), schema=None)<br>
ref2_table.append_constraint(pk)<br>
ref2_table<br>
Table('foo2', MetaData(None), Column('id_ref', Integer(), ForeignKey('users.id')<br>
, ForeignKey('users.id')), Column('name', String(length=30, convert_unicode=Fals<br>
e)), schema=None)</p>
</blockquote>
</blockquote>
</blockquote>
</li>
<li>
<p dir="auto">Even worse, you can add different Foreign Keys to the same Column</p>
<blockquote>
<blockquote>
<blockquote>
<p dir="auto">pk2 = ForeignKeyConstraint(<a href="'id_ref'">'id_ref'</a>,<a href="ref_table.c.id_ref">ref_table.c.id_ref</a>)<br>
ref2_table.append_constraint(pk2)<br>
ref2_table<br>
Table('foo2', MetaData(None), Column('id_ref', Integer(), ForeignKey('users.id')<br>
, ForeignKey('users.id'), ForeignKey('foo.id_ref')), Column('name', String(lengt<br>
h=30, convert_unicode=False)), schema=None)</p>
</blockquote>
</blockquote>
</blockquote>
</li>
</ol>