I'm working on a FastAPI project and I'm using SQLAlchemy to map the data to the API. I swear this problem started randomly as I've reverted back to my MVP code (bookmarked so I can always roll back to working code), and it still doesn't work.
My situation is this, I'm trying to query the database to get the record count and then offset/limit and paginate.
Here is my broken code:
query = db.query(query_class)
total_items = query.count().scalar()
This returns an error int() argument must be a string, a bytes-like object or a real number, not 'NoneType'
which tells me that count is likely None.
Now obviously I can query the dataset and see there are records. In fact I can even do this:
total_items = len(db.query(query_class).all())
Now can someone enlighten me on what I'm doing wrong here?
Here are my versions:
- SQLAlchemy 1.4.54
- sqlalchemy-databricks 0.2.0
I'm working on a FastAPI project and I'm using SQLAlchemy to map the data to the API. I swear this problem started randomly as I've reverted back to my MVP code (bookmarked so I can always roll back to working code), and it still doesn't work.
My situation is this, I'm trying to query the database to get the record count and then offset/limit and paginate.
Here is my broken code:
query = db.query(query_class)
total_items = query.count().scalar()
This returns an error int() argument must be a string, a bytes-like object or a real number, not 'NoneType'
which tells me that count is likely None.
Now obviously I can query the dataset and see there are records. In fact I can even do this:
total_items = len(db.query(query_class).all())
Now can someone enlighten me on what I'm doing wrong here?
Here are my versions:
- SQLAlchemy 1.4.54
- sqlalchemy-databricks 0.2.0
1 Answer
Reset to default 0This looks like it was just a version issue, which makes sense, as I was fixing my requirements file at some point, and my venv would not have reset when I rolled back.
This issue was resolved by updating the following libraries:
databricks-sql-connector==4.0.0
databricks-sqlalchemy==2.0.5
SQLAlchemy==2.0.39
query
a simpleSELECT
or somrthing more exotic, like aJOIN
? – snakecharmerb Commented Mar 25 at 7:11query.count()
return? Because.scalar()
is unnecessary here. – frisko Commented Mar 25 at 7:55db.query(func.count(query_class.id)).scalar()
to use the SQLsCOUNT
function instead. – MatsLindh Commented Mar 25 at 7:55.scalar()
on thecount()
result raisesAttributeError: 'int' object has no attribute 'scalar'
. Not the same as the OP'sTypeError
, but that could be a version difference. – snakecharmerb Commented Mar 25 at 10:43