Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper Way to Insert Strings to a SQLAlchemy Unicode Column

I have a SQLAlchemy model with a Unicode column. I sometimes insert unicode values to it (u'Value'), but also sometimes insert ASCII strings. What is the best way to go about this? When I insert ASCII strings with special characters I get this warning:

SAWarning: Unicode type received non-unicode bind param value ...

How do I avoid this? What is the proper way to insert my different types of strings?

like image 743
Raiders Avatar asked Apr 20 '11 18:04

Raiders


1 Answers

Thre are several options:

  1. Disable all SQLAlchemy warnings with warnings.simplefilter('ignore', sqlalchemy.exc.SAWarning).
  2. Disable only this warning with more specific filter spec by module and lineno or message, e.g. warnings.filterwarnings('ignore', '^Unicode type received non-unicode bind param value', sqlalchemy.exc.SAWarning).
  3. Use String(convert_unicode=True) instead of Unicode type.
  4. Rethink the problem and change your code to use unicode even for ASCII strings.
like image 132
Denis Otkidach Avatar answered Sep 30 '22 02:09

Denis Otkidach