MCPcopy
hub / github.com/django/django / set_name_with_model

Method set_name_with_model

django/db/models/indexes.py:250–283  ·  view source on GitHub ↗

Generate a unique name for the index. The name is divided into 3 parts - table name (12 chars), field name (8 chars) and unique hash + suffix (10 chars). Each part is made to fit its size by truncating the excess length.

(self, model)

Source from the content-addressed store, hash-verified

248 return self.__class__(*args, **kwargs)
249
250 def set_name_with_model(self, model):
251 """
252 Generate a unique name for the index.
253
254 The name is divided into 3 parts - table name (12 chars), field name
255 (8 chars) and unique hash + suffix (10 chars). Each part is made to
256 fit its size by truncating the excess length.
257 """
258 _, table_name = split_identifier(model._meta.db_table)
259 column_names = [
260 model._meta.get_field(field_name).column
261 for field_name, order in self.fields_orders
262 ]
263 column_names_with_order = [
264 (("-%s" if order else "%s") % column_name)
265 for column_name, (field_name, order) in zip(
266 column_names, self.fields_orders
267 )
268 ]
269 # The length of the parts of the name is based on the default max
270 # length of 30 characters.
271 hash_data = [table_name, *column_names_with_order, self.suffix]
272 self.name = "%s_%s_%s" % (
273 table_name[:11],
274 column_names[0][:7],
275 "%s_%s" % (names_digest(*hash_data, length=6), self.suffix),
276 )
277 if len(self.name) > self.max_name_length:
278 raise ValueError(
279 "Index too long for multiple database support. Is self.suffix "
280 "longer than 3 characters?"
281 )
282 if self.name[0] == "_" or self.name[0].isdigit():
283 self.name = "D%s" % self.name[1:]
284
285 def __repr__(self):
286 return "<%s:%s%s%s%s%s%s%s>" % (

Calls 3

split_identifierFunction · 0.90
names_digestFunction · 0.90
get_fieldMethod · 0.45