On Jul 7, 8:56 am, Michael Bayer <[email protected]> wrote:
> On Jul 7, 2010, at 1:30 AM, Lance Edgar wrote:
>
>
>
> > Hi, I was wondering what method might be used (assuming it's possible)
> > to redefine a column's attribute type after the mapping has already
> > been made? Specifically I have the following scenario:
>
> > from sqlalchemy import *
> > from sqlalchemy.orm import mapper
>
> > metadata = MetaData()
>
> > orders = Table(
> > "orders", metadata,
> > Column(id, Integer, primary_key=True),
> > Column(quantity, Numeric(8,3)),
> > )
>
> > class Order(object):
> > pass
>
> > mapper(Order, orders)
>
> > # ... then later ...
>
> > from sqlalchemy.orm import class_mapper
>
> > class_mapper(Order).get_property('quantity').update_to_integer_type()
>
> > Obviously that last method doesn't exist, but I would like something
> > along those lines. Basically I want to type-cast the column at the
> > ORM layer instead of everywhere it appears in the UI. I can't just
> > pass Integer to the Column definition because that's happening
> > elsewhere in another package. Thanks in advance for any suggestions.
>
> map it as column_property(cast(table.c.column, Integer)).
Michael, thanks for the tip. I still found this somewhat confusing
though:
When my code runs, the mapper has already been created (and
"compiled", I assume). So what I ended up doing, that seemed to work,
is:
class_mapper(Order).add_property('quantity',
column_property(cast(orders.c.quantity, Integer)))
The thing is, the mapper *already* had a "quantity" property so
without knowing the internals of that business I can only assume that
my .add_property() call doesn't have weird side effects (although it
does accomplish what I'm after). The mapper exposes .get_property()
and .iterate_properties(), and of course .add_property(), but I guess
I would have expected there to be a .set_property()
or .update_property(). Is this just a quirk in the wording or my
understanding, or am I still missing some important step?
Lance
--
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.