admin管理员组

文章数量:1401176

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
Share Improve this question edited Mar 25 at 17:37 snakecharmerb 56.2k13 gold badges134 silver badges187 bronze badges asked Mar 25 at 2:37 Robert RileyRobert Riley 4061 gold badge14 silver badges38 bronze badges 6
  • What version of SQLAlchemy are you using? Also is query a simple SELECT or somrthing more exotic, like a JOIN? – snakecharmerb Commented Mar 25 at 7:11
  • 1 A more complete minimal reproducible example would be helpful too. While it's not impossible that this is a bug in SQLALchemy, it's much more likely to be a bug in your code. – snakecharmerb Commented Mar 25 at 7:22
  • 1 What does query.count() return? Because .scalar() is unnecessary here. – frisko Commented Mar 25 at 7:55
  • 1 Generally you'd want db.query(func.count(query_class.id)).scalar() to use the SQLs COUNT function instead. – MatsLindh Commented Mar 25 at 7:55
  • @frisko good spot, for me calling .scalar() on the count() result raises AttributeError: 'int' object has no attribute 'scalar'. Not the same as the OP's TypeError, but that could be a version difference. – snakecharmerb Commented Mar 25 at 10:43
 |  Show 1 more comment

1 Answer 1

Reset to default 0

This 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

本文标签: pythonSQLAlchemy Count returns NoneTypeStack Overflow