jfinkels / flask-restless

NO LONGER MAINTAINED - A Flask extension for creating simple ReSTful JSON APIs from SQLAlchemy models.
https://flask-restless.readthedocs.io
GNU Affero General Public License v3.0
1.02k stars 301 forks source link

Exception trying to jsonify decimal count #694

Open tpow opened 5 years ago

tpow commented 5 years ago

I'm trying to use flask-restless against an Informix database. Informix returns a decimal type for count. For example, "select count(id) as c1 from users" would return c1 as Decimal('26'). Because of this, flask-restless throws an exception when trying to create the JSON of num_results. I'm finding this against the 0.17 version.

Even though Informix returns a Decimal type for the count, it looks like it could be safely cast to an Int in the context of generating num_results. I see two possible files where this could be addressed: in the helpers.py in the count function the returns could be cast to int. Second option, and perhaps better, change views.py in the _pagination function where it looks up num_results = count... I added the cast as num_results = int(count...) in this file and that seems to fix the problem for Informix. It looks like the equivalent code for this is views/base.py in the 1.0.0 beta code. Based on skimming the code changes, this might be partially fixed by #363 when not using pagination, but the cast to int may still be helpful otherwise.

Based on researching this problem, it sounds like Oracle databases can also return Decimal types for count.

Tim

reubano commented 4 years ago

The issue is with Flask, see my SO answer.

tpow commented 4 years ago

I don't think so, but even if so, Flask-restless should handle the return data type.