1
0
mirror of https://github.com/django/django.git synced 2025-01-03 06:55:47 +00:00

Made inspectdb used Cursor.description.display_size for CharFields' max_length.

internal_size is size for fixed-size types not for char types.
This commit is contained in:
Mariusz Felisiak 2022-12-08 09:00:35 +01:00 committed by GitHub
parent 95a101a690
commit cbc0fb3705
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 19 additions and 12 deletions

View File

@ -328,8 +328,8 @@ class Command(BaseCommand):
field_notes.append("This field type is a guess.") field_notes.append("This field type is a guess.")
# Add max_length for all CharFields. # Add max_length for all CharFields.
if field_type == "CharField" and row.internal_size: if field_type == "CharField" and row.display_size:
field_params["max_length"] = int(row.internal_size) field_params["max_length"] = int(row.display_size)
if field_type in {"CharField", "TextField"} and row.collation: if field_type in {"CharField", "TextField"} and row.collation:
field_params["db_collation"] = row.collation field_params["db_collation"] = row.collation

View File

@ -148,7 +148,8 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
info = field_info[line[0]] info = field_info[line[0]]
fields.append( fields.append(
FieldInfo( FieldInfo(
*line[:3], *line[:2],
to_int(info.max_len) or line[2],
to_int(info.max_len) or line[3], to_int(info.max_len) or line[3],
to_int(info.num_prec) or line[4], to_int(info.num_prec) or line[4],
to_int(info.num_scale) or line[5], to_int(info.num_scale) or line[5],

View File

@ -116,7 +116,7 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
WHEN user_tab_cols.char_used IS NULL WHEN user_tab_cols.char_used IS NULL
THEN user_tab_cols.data_length THEN user_tab_cols.data_length
ELSE user_tab_cols.char_length ELSE user_tab_cols.char_length
END as internal_size, END as display_size,
CASE CASE
WHEN user_tab_cols.identity_column = 'YES' THEN 1 WHEN user_tab_cols.identity_column = 'YES' THEN 1
ELSE 0 ELSE 0
@ -141,7 +141,7 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
) )
field_map = { field_map = {
column: ( column: (
internal_size, display_size,
default if default != "NULL" else None, default if default != "NULL" else None,
collation, collation,
is_autofield, is_autofield,
@ -151,7 +151,7 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
column, column,
default, default,
collation, collation,
internal_size, display_size,
is_autofield, is_autofield,
is_json, is_json,
) in cursor.fetchall() ) in cursor.fetchall()
@ -165,13 +165,14 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
description = [] description = []
for desc in cursor.description: for desc in cursor.description:
name = desc[0] name = desc[0]
internal_size, default, collation, is_autofield, is_json = field_map[name] display_size, default, collation, is_autofield, is_json = field_map[name]
name %= {} # cx_Oracle, for some reason, doubles percent signs. name %= {} # cx_Oracle, for some reason, doubles percent signs.
description.append( description.append(
FieldInfo( FieldInfo(
self.identifier_converter(name), self.identifier_converter(name),
*desc[1:3], desc[1],
internal_size, display_size,
desc[3],
desc[4] or 0, desc[4] or 0,
desc[5] or 0, desc[5] or 0,
*desc[6:], *desc[6:],

View File

@ -113,7 +113,8 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
FieldInfo( FieldInfo(
line.name, line.name,
line.type_code, line.type_code,
line.display_size, # display_size is always None on psycopg2.
line.internal_size if line.display_size is None else line.display_size,
line.internal_size, line.internal_size,
line.precision, line.precision,
line.scale, line.scale,

View File

@ -119,10 +119,10 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
FieldInfo( FieldInfo(
name, name,
data_type, data_type,
None,
get_field_size(data_type), get_field_size(data_type),
None, None,
None, None,
None,
not notnull, not notnull,
default, default,
collations.get(name), collations.get(name),

View File

@ -409,6 +409,10 @@ Miscellaneous
* The minimum supported version of SQLite is increased from 3.9.0 to 3.21.0. * The minimum supported version of SQLite is increased from 3.9.0 to 3.21.0.
* :djadmin:`inspectdb` now uses ``display_size`` from
``DatabaseIntrospection.get_table_description()`` rather than
``internal_size`` for ``CharField``.
.. _deprecated-features-4.2: .. _deprecated-features-4.2:
Features deprecated in 4.2 Features deprecated in 4.2

View File

@ -132,7 +132,7 @@ class IntrospectionTests(TransactionTestCase):
) )
self.assertEqual( self.assertEqual(
[ [
r[3] r[2]
for r in desc for r in desc
if connection.introspection.get_field_type(r[1], r) == "CharField" if connection.introspection.get_field_type(r[1], r) == "CharField"
], ],