#36030: Expressions that divide an integer field by a constant decimal.Decimal
returns inconsistent decimal places on SQLite
-------------------------------------+-------------------------------------
     Reporter:  Bartłomiej Nowak     |                    Owner:  Gregory
                                     |  Mariani
         Type:  Bug                  |                   Status:  assigned
    Component:  Database layer       |                  Version:  dev
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:  division             |             Triage Stage:  Accepted
  decimalfield                       |
    Has patch:  1                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  1
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Comment (by Jacob Walls):

 I was using some shorthand in my reply, so it's totally fair to ask for
 clarification (happy to), but it looks like you echoed back LLM output
 mostly verbatim. I can do that myself; it doesn't need to be posted on
 Trac. I would have appreciated you condensing your reply and writing it in
 your own voice.

 > What the ticket is highlighting is that the ORM currently doesn’t
 preserve that invariant:

 Actually, it's not the ORM, it's the database adapter.

 Django sends `Decimal` objects to psycopg2. For the query
 `Book.objects.filter(price=42).annotate(new_price=F("pages") /
 Decimal(28))`, the ORM sends `Decimal` objects to the adapter:

 {{{#!py
 (Pdb) params
 (Decimal('28'), Decimal('42'))
 (Pdb) sql
 'SELECT ("annotations_book"."pages" / %s) AS "new_price" FROM
 "annotations_book" WHERE "annotations_book"."price" = %s LIMIT 21'
 }}}

 ... and psycopg2 makes the reasonable choice to mogrify `Decimal(28)` to
 "28". If that is undesired, there are multiple ways to fix this, including
 adding an explicit cast, or by using string formatting to preserve the
 precision.

 I don't think `Decimal(28)` or `Decimal(float_var)` is standard usage that
 we should memorialize in the docs. We don't document all database quirks.
 This ticket should be enough. Hope that helps.
-- 
Ticket URL: <https://code.djangoproject.com/ticket/36030#comment:25>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/0107019aa18fee71-64809921-6845-4381-bb02-485b8767bab4-000000%40eu-central-1.amazonses.com.

Reply via email to