简体   繁体   中英

SQLAlchemy query raises unnecessary warning about sqlite and Decimal, how to specifically disable?

I'm using SQLAlchemy via pyramid accessing a simple sqlite database.

As I was building my progressively more complex queries, I got this warning:-

SAWarning: Dialect sqlite+pysqlite does not support Decimal objects natively, and SQLAlchemy must convert from floating point - rounding errors and other issues may occur. Please consider storing Decimal numbers as strings or integers on this platform for lossless storage.

One of my queries does this (paraphrasing):-

session.query(subquery.c.a*100.0/(subquery.c.b+subquery.c.c))

In this case, subquery is the name of a subquery, and a, b, and c are integers.

I'm not saving the result of the query or running any commits, this is purely data extraction/querying, hence I was surprised when the warning above was raised. How do I ignore this warning without filtering it out totally (in case in future I do accidentally save Decimal data to the database)?

You can ignore warnings with warnings.filterwarnings as done here :

import warnings
from sqlalchemy.exc import SAWarning
warnings.filterwarnings('ignore',
 r"^Dialect sqlite\+pysqlite does \*not\* support Decimal objects natively\, "
 "and SQLAlchemy must convert from floating point - rounding errors and other "
 "issues may occur\. Please consider storing Decimal numbers as strings or "
 "integers on this platform for lossless storage\.$",
 SAWarning, r'^sqlalchemy\.sql\.type_api$')

Edit: The problem of using decimal values with SQLite and SQLAlchemy is already discussed in this question , and this answer suggests to create TypeDecorator that stores the decimal values internally as text.

For the division by 100.0 in your query it seems that this value is converted to Decimal by SQLAlchemy. However you can create a literal_column with 100.0 as value:

import sqlalchemy
val100 = sqlalchemy.sql.expression.literal_column("100.0")
session.query(subquery.c.a*val100/(subquery.c.b+subquery.c.c))

When using this solution it does not show a warning for me.

PD: I use SQLAlchemy version 1.0.11, and Python 2.7.11.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM