diff options
| author | Tom Carrick <tom@carrick.eu> | 2020-07-18 13:17:39 +0200 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2020-09-21 18:24:56 +0200 |
| commit | e387f191f76777015b6ea687ce83cdb05ee47cee (patch) | |
| tree | 003d83b5efda40fbfcdc1aa9302faca9578b1e30 /django/db/backends/sqlite3 | |
| parent | ba6b32e5efc4c813ba4432777b3b1743d4205d14 (diff) | |
Fixed #31777 -- Added support for database collations to Char/TextFields.
Thanks Simon Charette and Mariusz Felisiak for reviews.
Diffstat (limited to 'django/db/backends/sqlite3')
| -rw-r--r-- | django/db/backends/sqlite3/features.py | 1 | ||||
| -rw-r--r-- | django/db/backends/sqlite3/introspection.py | 27 | ||||
| -rw-r--r-- | django/db/backends/sqlite3/schema.py | 3 |
3 files changed, 30 insertions, 1 deletions
diff --git a/django/db/backends/sqlite3/features.py b/django/db/backends/sqlite3/features.py index e879e049f9..59b8d6862f 100644 --- a/django/db/backends/sqlite3/features.py +++ b/django/db/backends/sqlite3/features.py @@ -47,6 +47,7 @@ class DatabaseFeatures(BaseDatabaseFeatures): test_collations = { 'ci': 'nocase', 'cs': 'binary', + 'non_default': 'nocase', } @cached_property diff --git a/django/db/backends/sqlite3/introspection.py b/django/db/backends/sqlite3/introspection.py index 3cbf844559..1d7ce3fabf 100644 --- a/django/db/backends/sqlite3/introspection.py +++ b/django/db/backends/sqlite3/introspection.py @@ -84,6 +84,7 @@ class DatabaseIntrospection(BaseDatabaseIntrospection): """ cursor.execute('PRAGMA table_info(%s)' % self.connection.ops.quote_name(table_name)) table_info = cursor.fetchall() + collations = self._get_column_collations(cursor, table_name) json_columns = set() if self.connection.features.can_introspect_json_field: for line in table_info: @@ -102,7 +103,7 @@ class DatabaseIntrospection(BaseDatabaseIntrospection): return [ FieldInfo( name, data_type, None, get_field_size(data_type), None, None, - not notnull, default, pk == 1, name in json_columns + not notnull, default, collations.get(name), pk == 1, name in json_columns ) for cid, name, data_type, notnull, default, pk in table_info ] @@ -435,3 +436,27 @@ class DatabaseIntrospection(BaseDatabaseIntrospection): } constraints.update(self._get_foreign_key_constraints(cursor, table_name)) return constraints + + def _get_column_collations(self, cursor, table_name): + row = cursor.execute(""" + SELECT sql + FROM sqlite_master + WHERE type = 'table' AND name = %s + """, [table_name]).fetchone() + if not row: + return {} + + sql = row[0] + columns = str(sqlparse.parse(sql)[0][-1]).strip('()').split(', ') + collations = {} + for column in columns: + tokens = column[1:].split() + column_name = tokens[0].strip('"') + for index, token in enumerate(tokens): + if token == 'COLLATE': + collation = tokens[index + 1] + break + else: + collation = None + collations[column_name] = collation + return collations diff --git a/django/db/backends/sqlite3/schema.py b/django/db/backends/sqlite3/schema.py index 146ad18f84..6a2c887612 100644 --- a/django/db/backends/sqlite3/schema.py +++ b/django/db/backends/sqlite3/schema.py @@ -429,3 +429,6 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): super().remove_constraint(model, constraint) else: self._remake_table(model) + + def _collate_sql(self, collation): + return ' COLLATE ' + collation |
