mirror of https://github.com/django/django.git
Fixed #31815 -- Fixed schema value encoding on PostgreSQL.
This commit is contained in:
parent
c1f8d87bb0
commit
f4e93919e4
|
@ -38,8 +38,11 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor):
|
|||
def quote_value(self, value):
|
||||
if isinstance(value, str):
|
||||
value = value.replace('%', '%%')
|
||||
adapted = psycopg2.extensions.adapt(value)
|
||||
if hasattr(adapted, 'encoding'):
|
||||
adapted.encoding = 'utf8'
|
||||
# getquoted() returns a quoted bytestring of the adapted value.
|
||||
return psycopg2.extensions.adapt(value).getquoted().decode()
|
||||
return adapted.getquoted().decode()
|
||||
|
||||
def _field_indexes_sql(self, model, field):
|
||||
output = super()._field_indexes_sql(model, field)
|
||||
|
|
|
@ -4,6 +4,7 @@ from django.db import models
|
|||
class Product(models.Model):
|
||||
price = models.IntegerField(null=True)
|
||||
discounted_price = models.IntegerField(null=True)
|
||||
unit = models.CharField(max_length=15, null=True)
|
||||
|
||||
class Meta:
|
||||
required_db_features = {
|
||||
|
@ -31,6 +32,13 @@ class Product(models.Model):
|
|||
),
|
||||
name='%(app_label)s_price_neq_500_wrap',
|
||||
),
|
||||
models.CheckConstraint(
|
||||
check=models.Q(
|
||||
models.Q(unit__isnull=True) |
|
||||
models.Q(unit__in=['μg/mL', 'ng/mL'])
|
||||
),
|
||||
name='unicode_unit_list',
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
|
|
|
@ -88,6 +88,12 @@ class CheckConstraintTests(TestCase):
|
|||
with self.assertRaises(IntegrityError):
|
||||
Product.objects.create(price=10, discounted_price=20)
|
||||
|
||||
@skipUnlessDBFeature('supports_table_check_constraints')
|
||||
def test_database_constraint_unicode(self):
|
||||
Product.objects.create(price=10, discounted_price=5, unit='μg/mL')
|
||||
with self.assertRaises(IntegrityError):
|
||||
Product.objects.create(price=10, discounted_price=7, unit='l')
|
||||
|
||||
@skipUnlessDBFeature('supports_table_check_constraints')
|
||||
def test_database_constraint_expression(self):
|
||||
Product.objects.create(price=999, discounted_price=5)
|
||||
|
|
Loading…
Reference in New Issue