I\'m using pyramid for a web application with a postgres database, wtforms, sqlalchemy and jinja2 and I\'m having this error when the application try to get the issues types fro
I use mysql and set the charset like this. It works for me.
from sqlalchemy import create_engine
from sqlalchemy.engine.url import URL
db_url = {
'database': 'db_name',
'drivername': 'mysql',
'username': 'username',
'password': 'mypassword',
'host': '127.0.0.1',
'query': {'charset': 'utf8'},
}
engine = create_engine(URL(**db_url), encoding="utf8")
You need to configure Psycopg2's client encoding. See the SQLAlchemy documentation:
By default, the psycopg2 driver uses the
psycopg2.extensions.UNICODE
extension, such that the DBAPI receives and returns all strings as Python Unicode objects directly - SQLAlchemy passes these values through without change. Psycopg2 here will encode/decode string values based on the current “client encoding” setting; by default this is the value in thepostgresql.conf
file, which often defaults toSQL_ASCII
. Typically, this can be changed toutf-8
, as a more useful default:#client_encoding = sql_ascii # actually, defaults to database # encoding client_encoding = utf8
A second way to affect the client encoding is to set it within Psycopg2 locally. SQLAlchemy will call psycopg2’s
set_client_encoding()
method (see: http://initd.org/psycopg/docs/connection.html#connection.set_client_encoding) on all new connections based on the value passed tocreate_engine()
using theclient_encoding
parameter:engine = create_engine("postgresql://user:pass@host/dbname", client_encoding='utf8')
This overrides the encoding specified in the Postgresql client configuration.
The client_encoding
parameter can be specified as a query string in the engine URL:
postgresql://user:pass@host/dbname?client_encoding=utf8