Issue 1:
Migrated issue, originally created by Anonymous

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](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()

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

Issue 2:
Migrated issue, originally created by Anonymous

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:

Appending a broken constraint to a table (very degenerated case...) creates a cryptic error message..., should probably tell me that my PrimaryKeyConstraint is broken...

from sqlalchemy import *

meta = MetaData()

users_table = Table('users',meta,

... Column('id',Integer,primary_key=True))

ref_table = Table('foo',meta,Column('id_ref',Integer))

pk = PrimaryKeyConstraint('id_ref',users_table.c.id)

ref_table.append_constraint(pk)

Traceback (most recent call last):

File "", line 1, in

File "c:\sdk\win32\1.0\Python25\lib\site-packages\sqlalchemy-0.4.1-py2.5.egg\s

qlalchemy\schema.py", line 270, in append_constraint

constraint._set_parent(self)

File "c:\sdk\win32\1.0\Python25\lib\site-packages\sqlalchemy-0.4.1-py2.5.egg\s

qlalchemy\schema.py", line 902, in _set_parent

self.append_column(table.cc)

File "c:\sdk\win32\1.0\Python25\lib\site-packages\sqlalchemy-0.4.1-py2.5.egg\s

qlalchemy\util.py", line 275, in getitem

return self._datakey

TypeError: list objects are unhashable

Adding the same constraint more than once does not weed out duplicates, it just adds the ForeignKey() to the Column...

pk = ForeignKeyConstraint('id_ref',users_table.c.id)

ref2_table.append_constraint(pk)

ref2_table

Table('foo2', MetaData(None), Column('id_ref', Integer(), ForeignKey('users.id')

), Column('name', String(length=30, convert_unicode=False)), schema=None)

ref2_table.append_constraint(pk)

ref2_table

Table('foo2', MetaData(None), Column('id_ref', Integer(), ForeignKey('users.id')

, ForeignKey('users.id')), Column('name', String(length=30, convert_unicode=Fals

e)), schema=None)

Even worse, you can add different Foreign Keys to the same Column

pk2 = ForeignKeyConstraint('id_ref',ref_table.c.id_ref)

ref2_table.append_constraint(pk2)

ref2_table

Table('foo2', MetaData(None), Column('id_ref', Integer(), ForeignKey('users.id')

, ForeignKey('users.id'), ForeignKey('foo.id_ref')), Column('name', String(lengt

h=30, convert_unicode=False)), schema=None)