2 ms·
> it's pretty easy to figure out exactly what the expression builder is doing I don't know about that. I touched SQLAlchemy again this very morning for the fir
by weberc2 10y ago
> it's pretty easy to figure out exactly what the expression builder is doing
I don't know about that. I touched SQLAlchemy again this very morning for the first time in several months. I was just trying to get the count of a table with several rows, so I ran `table.count()`, but it kept returning 0. The problem happened on MySQL and Postgres. It turns out that this doesn't compile to `select count()`, but instead it returns the count of the first column in the SQLAlchemy table object's column list. In my case, that column was all NULL, so the count was coming back as zero.
I thought maybe this was an accident, but here's the body of the count() function from SQLAlchemy's source:
```
if self.primary_key:
col = list(self.primary_key)[0]
else:
col = list(self.columns)[0]
return Select(
[functions.func.count(col).label('tbl_row_count')],
whereclause,
from_obj=[self],
*params)
```
I run into this sort of unintuitive behavior every time I touch this library. :(