Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Numeric with as_decimal=False are converted in the wrong colander type #108

Open
tonthon opened this issue Jun 18, 2019 · 3 comments
Open

Comments

@tonthon
Copy link

tonthon commented Jun 18, 2019

Given the following model

class MyModel(Base):
    id = Column(Integer, primary_key=True)
    quantity = Numeric(7,5, as_decimal=False)

The generated schema node for quantity is of type colander.Decimal

from colanderalchemy import SQLAlchemySchemaNode
schema = SQLAlchemySchemaNode(MyModel, includes=('quantity',))
schema['quantity'].typ
<colander.Decimal at 0x7f4b840f2a90>

It should be colander.Float.

In my case I found a turnaround by setting the typ afterwards.

@tisdall
Copy link
Collaborator

tisdall commented Jun 18, 2019

Looking at ColanderAlchemy it looks like it ignores the asdecimal flag. You could submit a PR, but no one is actively maintaining this library right now.

Why don't you use Float or Numeric without the asdecimal setting? I don't really understand the purpose of switching back and forth between float and Decimal (which will essentially lose precision).

@tonthon
Copy link
Author

tonthon commented Aug 7, 2019

I'll try to provide a PR if I find time for it.

We only use Decimal in the computation modules to avoid rounding problems. It works well this way.
Switching to another behaviour would be a large work that is far to be a priority on our roadmap.

In our app, we always customize the generated colander schema when asking for it, we added a hack in that part to solve our problem.

@tisdall
Copy link
Collaborator

tisdall commented Aug 7, 2019

Maybe I'm misunderstanding, but if you're using Decimal then use use Numeric and keep everything as a Decimal. If you use Numeric(7,5, asdecimal=False) then you're converting to a float and can possibly lose precision. In general it seems like you should use Numeric when you want to do everything in Decimal and Float when you want to do everything in float. The asdecimal flag causes a type coercion (it seems like it's purpose is for database engines that don't support particular data types).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants