Issue: count() and exists() don't work when primary_key is a CompositeKey
Author: PatSmuk
State: closed
Created: 2014-05-08 21:57:47

I have a table that has its primary_key set to a CompositeKey, and attempting to use count() or exists() with that table causes a **peewee.ProgrammingError**.

Here's the trace:

```
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\flask\app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Python27\lib\site-packages\flask\app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "C:\Python27\lib\site-packages\flask\app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Python27\lib\site-packages\flask\app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Python27\lib\site-packages\flask\app.py", line 1477, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Python27\lib\site-packages\flask\app.py", line 1381, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Python27\lib\site-packages\flask\app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Python27\lib\site-packages\flask\app.py", line 1461, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Users\Pat\Source\blackcoinpool\flask_site\app.py", line 213, in pool_stats
    Stat.select().where(Stat.server == server).where(Stat.time > five_minutes_ago).exists()
  File "C:\Python27\lib\site-packages\peewee.py", line 2035, in exists
    return bool(clone.scalar())
  File "C:\Python27\lib\site-packages\peewee.py", line 1805, in scalar
    row = self._execute().fetchone()
  File "C:\Python27\lib\site-packages\peewee.py", line 1796, in _execute
    return self.database.execute_sql(sql, params, self.require_commit)
  File "C:\Python27\lib\site-packages\peewee.py", line 2359, in execute_sql
    self.commit()
  File "C:\Python27\lib\site-packages\peewee.py", line 2230, in __exit__
    reraise(new_type, new_type(*exc_value.args), traceback)
  File "C:\Python27\lib\site-packages\peewee.py", line 2351, in execute_sql
    cursor.execute(sql, params or ())
ProgrammingError: can't adapt type 'CompositeKey'
```

And the Model itself:

``` python
class Stat(db.Model):
    server = ForeignKeyField(db_column='server_id', rel_model=Server)
    coin = ForeignKeyField(db_column='coin_id', rel_model=Coin)
    hash_rate = DecimalField(default=0)
    hash_type = ForeignKeyField(db_column='hash_type_id', rel_model=HashType)
    share_count = DecimalField(default=0)
    time = DateTimeField()
    worker_count = IntegerField(default=0)

    class Meta:
        db_table = 'stats'
        primary_key = CompositeKey('server', 'time', 'coin')
```


Is this example code related to bitcoin in some way?


It is. We're using peewee to power [our website](http://blackcoinpool.com/). If you drop a wallet address I'd be happy to send some Bitcoin or Blackcoin your way. :smiley: 


> It is. We're using peewee to power our website. If you drop a wallet address I'd be happy to send some Bitcoin or Blackcoin your way. 

Haha awesome, I appreciate that. I don't have a wallet but thank you.


Just thought I'd re-open the issue because I just encountered the same problem with count() and flask-peewee's admin system.


I have the same erron in count().


Currently count is implemented as the [following](https://github.com/coleifer/peewee/blob/master/peewee.py#L2068-2073): 

```
def count(self):
    if self._distinct or self._group_by:
        return self.wrapped_count()

     # defaults to a count() of the primary key
     return self.aggregate(convert=False) or 0
```

What is the purpose that it "defaults to a count() of the primary key" rather than `COUNT(*)`?


Thanks @coleifer !