Skip to content

Commit e938360

Browse files
committed
add column comment to get_table_description()
1 parent 068c7fc commit e938360

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

mssql/introspection.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from django import VERSION
1010
from django.db.backends.base.introspection import BaseDatabaseIntrospection
11-
from django.db.backends.base.introspection import FieldInfo
11+
from django.db.backends.base.introspection import FieldInfo as BaseFieldInfo
1212
from django.db.backends.base.introspection import TableInfo as BaseTableInfo
1313
from django.db.models.indexes import Index
1414
from django.conf import settings
@@ -17,6 +17,7 @@
1717
SQL_BIGAUTOFIELD = -777444
1818
SQL_TIMESTAMP_WITH_TIMEZONE = -155
1919

20+
FieldInfo = namedtuple("FieldInfo", BaseFieldInfo._fields + ("comment",))
2021
TableInfo = namedtuple("TableInfo", BaseTableInfo._fields + ("comment",))
2122

2223
def get_schema_name():
@@ -144,15 +145,27 @@ def get_table_description(self, cursor, table_name, identity_check=True):
144145
column.append(collation_name[0] if collation_name else '')
145146
else:
146147
column.append('')
147-
148+
if VERSION >= (4, 2):
149+
sql = """select CAST(ep.value AS VARCHAR) AS COMMENT
150+
FROM sys.columns c
151+
INNER JOIN sys.tables t ON c.object_id = t.object_id
152+
INNER JOIN sys.extended_properties ep ON c.object_id=ep.major_id AND ep.minor_id = c.column_id
153+
WHERE t.name = '%s' AND c.name = '%s' AND ep.name = 'MS_Description'
154+
""" % (table_name, column[0])
155+
cursor.execute(sql)
156+
comment = cursor.fetchone()
157+
column.append(comment)
148158
if identity_check and self._is_auto_field(cursor, table_name, column[0]):
149159
if column[1] == Database.SQL_BIGINT:
150160
column[1] = SQL_BIGAUTOFIELD
151161
else:
152162
column[1] = SQL_AUTOFIELD
153163
if column[1] == Database.SQL_WVARCHAR and column[3] < 4000:
154164
column[1] = Database.SQL_WCHAR
155-
items.append(FieldInfo(*column))
165+
if VERSION >= (4, 2):
166+
items.append(FieldInfo(*column))
167+
else:
168+
items.append(BaseFieldInfo(*column))
156169
return items
157170

158171
def get_sequences(self, cursor, table_name, table_fields=()):

0 commit comments

Comments
 (0)