Opened 4 years ago
Closed 4 years ago
#31815 closed Bug (fixed)
CheckConstraint() with unicode parameters crashes on PostgreSQL.
Reported by: | JSAustin | Owned by: | Mariusz Felisiak |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | encoding utf-8 |
Cc: | Simon Charette | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
I am trying to create a model with a constraint on a field that uses UTF-8 encodings. Specifically, I'm trying to constrain scientific unit strings. The problematic one is: 'µg/mL'. The µ symbol has a b'\xc2\xb5' encoding with utf-8. When I add this as one of the check constraints, I get this error:
File "D:\DjangoSite\venv\lib\site-packages\django\db\backends\postgresql\schema.py", line 38, in quote_value
return psycopg2.extensions.adapt(value).getquoted().decode()
UnicodeEncodeError: 'latin-1' codec can't encode character '\u03bc' in position 0: ordinal not in range(256)
The migration is:
migrations.AddConstraint(model_name='biomarkertestinfo',constraint=models.CheckConstraint(check=models.Q(unitin=['μg/mL', 'ng/mL', 'pg/mL', 'U/L', 'mIU/mL', 'kIU/mL']), name='constrained biomarker unit choices'),)
I am running PostgreSQL 12 locally for this with psycopg2 connection.
Attachments (1)
Change History (6)
comment:1 by , 4 years ago
Description: | modified (diff) |
---|
comment:2 by , 4 years ago
Cc: | added |
---|---|
Summary: | Can't Create PostgreSQL Database Constraints with non latin-1 encodings → CheckConstraint() with unicode parameters crashes on PostgreSQL. |
Triage Stage: | Unreviewed → Accepted |
Version: | 3.0 → master |
comment:3 by , 4 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
Thanks for this ticket. I attached a regression test. The following patch (based on discussion) fixes this issue for me:
Can you confirm that it works for you?
Reproduced at f65454801bfa13fc043fee0aca8f49af41380683.