Just a follow-up,  the suggested work-around works great.  I had to move the
column_properties outside the class definition for some of them to work,
but the final result works fine (classes not shown but the definitions are
pretty obvious):

def IF(condition, true, false):
    """ Construct a simple case expression """
    return case([(condition, true)], else_ = false)

O = Order
L = OrderLine
P = OrderPayment
N = Numeric(14,2)
N.get_col_spec = lambda: "NUMERIC(14,2)"

def total(taxable):
    """ Helper to define total column properties """

    expr = L.quantity * L.price
    if taxable is not None:
        expr = IF(taxable == L.taxable, expr, 0)
    return func.sum(cast(expr, N))

O.taxable = column_property(
    select([total(True)], L.order_id == O.id).label("taxable"))

O.nontax = column_property(
    select([total(False)], L.order_id == O.id).label("nontax"))

O.subtotal = column_property(
    select([total(None)], L.order_id == O.id).label("subtotal"))

O.sales_tax = column_property(
    cast(func.round(O.taxable * O.tax_rate / 100, 2), N).label("sales_tax"))

O.total = column_property(
    select([O.subtotal + O.sales_tax]).label("total"))

O.discount = column_property(
    cast(0.00, N).label("discount"))

O.payments = column_property(
    select([func.sum(P.amount)], P.order_id == O.id).label("payments"))

O.change_due = column_property(
    select([O.payments - O.total]).label("change_due"))


del O, L, P, N, total


Thanks once again for your assistance.


Shawn

On Sun, Mar 22, 2009 at 1:14 PM, Shawn Church <[email protected]>wrote:

>
>
> On Sun, Mar 22, 2009 at 12:39 PM, Michael Bayer 
> <[email protected]>wrote:
>
>>
>>
>> On Mar 22, 2009, at 3:00 PM, Shawn Church wrote:
>>
>> >
>> > This isn't really the same thing because the float would be rounded
>> > after the sum not line by line.  I think I could also use sum(...
>> > type_ = Numeric(14,2)) for the same result.  Am I missing an easier
>> > way to do this?
>>
>> its an SQLA bug that will require a workaround until 0.6.  another
>> approach is monkeypatching a get_col_spec() on your Numeric type:
>>
>> n = Numeric(14, 2)
>> n.get_col_spec = lambda: "NUMERIC(14, 2)"
>>
>> that will work throughout 0.5.  in 0.6 it will not be needed but also
>> have no effect.
>>
>>
> Thanks,  this seems a reasonable workaround.  But would I cast(..., n)?
>
>
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to