0

When inserting/updating to table, I need the values to be uppercased. I found this solution, but it does not work for me. My table:

class User(DBBase):
    tablename = 'user'
    ...
    birthplace = sa.Column(sa.String, nullable=True)

    @validates('birthplace')
    def convert_upper(self, key, value):
        return value.upper()

When i'm doing

conn.execute(User.__table__.insert({'birthplace': 'new york'}))

the 'new york' is being saved to db instead of expected 'NEW YORK'. Thanks for your help!

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
mr_bulrathi
  • 514
  • 7
  • 23
  • 1
    `User.__table__` is a SQLAlchemy `Table` object. The validator is part of your `User` object and the underlying table has no notion of the validator. The value would be converted to uppercase if you did `u = User(birthplace="new york")`, and then persisted the `u` object. – Gord Thompson Dec 22 '20 at 12:54
  • @GordThompson Thank you! – mr_bulrathi Dec 22 '20 at 13:03
  • @ShadowRanger updated the answer so the solution can be found [here](https://stackoverflow.com/questions/34321976/best-way-to-force-values-to-uppercase-in-sqlalchemy-field/65408511?noredirect=1#comment115639700_65408511) – mr_bulrathi Dec 22 '20 at 17:23
  • 1
    With the update on my answer to address this corner case, this is now a dupe of [Best way to force values to uppercase in sqlalchemy field](https://stackoverflow.com/questions/34321976/best-way-to-force-values-to-uppercase-in-sqlalchemy-field). Sadly, you didn't tag it python or python-3.x, so I can't dupehammer, but at least they're linked now for people who have your problem. – ShadowRanger Dec 22 '20 at 18:09

0 Answers0