Ah, after days of trying to figure it out, it turns out Postgres just won't let you cast a column when editing an existing view. I had to drop the view first and create a brand new one from scratch. Casting in SQLAlchemy worked but I think I'm going to go with the "backend" fix. Thanks for the help!
On Jul 5, 5:50 pm, Pongetti <[email protected]> wrote: > For some reason if I use CAST directly in PG, it complains "cannot > change data type of view column". Its rather cumbersome but it does > seem to at least work in SQLAlchemy. Thanks. > > On Jul 5, 3:43 pm, Michael Bayer <[email protected]> wrote: > > > any expression in SQL can be coerced into a particular type using > > casts. PG supports the SQL standard CAST operator which SQLAlchemy > > provides as expression.cast, i.e. > > > from sqlalchemy import cast, Integer > > > cast(somevalue + someothervalue, Integer) > > > This will instruct PG to cast the value as integer and also allow > > SQLAlchemy to treat the expression construct as an int in python. > > > On Jul 5, 1:53 pm, Pongetti <[email protected]> wrote: > > > > Hello, > > > > I have a View in a Postgres DB with some calculated columns. In > > > Postgres the column types are getting "lost", so essentially, despite > > > all the math being on integers, Postgres is spitting out columns with > > > the type Numeric instead of type Integer. > > > > Using SQLAlchemy, I am therefore getting values wrapped in Decimal > > > functions rather than the native Python Int type. So for example, I > > > get Decimal("4") instead of just 4. > > > > I have been using autoload but even if I override the column types in > > > SQLAlchemy to be Integers, I am still getting Decimal("n"). > > > > Any help on how I could best get a plain old int would be most > > > appreciated. The ORM is a bit overkill for my project, I really just > > > need the SQL Expression language, so a solution not involving the ORM > > > would be preferred, though I will use it if I need to. > > > > Thank you! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
