On Sun, Mar 22, 2009 at 8:17 AM, Michael Bayer <mike...@zzzcomputing.com>wrote:

>
> theres three layers to this issue.   the first is, yes as_scalar() is
> trying to compile the expression which I don't think there's a very
> good reason, so ill likely commit a change for that (it will result in
> a behavioral change regarding the namespace of "someselect.c" but im
> hoping not one anyone was relying upon that).


Sounds good


> the second layer is,
> if you're casting against  a constant value like below, or a column on
> the Order class, you don't need the select() - just say
> cast(...).label('foo') -


I'm not casting a constant (see below).  I just simplified the expression
until I found the specific function causing the error.

however, convenient operations like "print
> query" still wont work since they evaulate the cast() without a
> dialect.


I like using print for debugging but I can live without it.


> the third is, the whole "cast can't compile without a
> dialect" thing is going away in 0.6, but I can't make it work in 0.5
> without major changes.
>
> One particular workaround that would totally eliminate the issue until
> 0.6 comes out would be to use the dialect-specific type for now, i.e.
> MSNumeric (mysql), PGNumeric (postgres), SLNumeric (sqlite).
>
>
I'm using SQLIte for unit tests and MySQL for production so I can't see a
way to do this.
I found that I CAN cast the result of the select (references OrderLine
table, not shown):

def IF(condition, true, false):
    return case([(condition, true)], else_ = false)


class Order(Base):

   -- Skipping Column Definitions --

    L = OrderLine

    taxable = column_property(
        cast(select([func.sum(IF(L.taxable, L.quantity * L.price, 0))],
                L.order_id == id).as_scalar(),
Numeric(14.2)).label("taxable"))


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?


>
>
> On Mar 22, 2009, at 6:27 AM, Shawn Church wrote:
>
> > >>> import sqlalchemy
> >     >>> sqlalchemy.__version__
> >     '0.5.2'
> >
> >     >>> from sqlalchemy import *
> >     >>> from sqlalchemy.orm import relation, column_property,
> > sessionmaker
> >
> >     >>> from sqlalchemy.ext.declarative import declarative_base
> >     >>> Base = declarative_base()
> >     >>> Session = sessionmaker()
> >
> >     >>> class Order(Base):
> >     ...    __tablename__ = "orders"
> >     ...
> >     ...    id = Column(Integer, primary_key = True)
> >     ...
> >     ...    test = column_property(select([cast(1,
> > Numeric(14,2))]).label("test"))
>
>
> >
>

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

Reply via email to