MCPcopy
hub / github.com/django/django / _iter_column_sql

Method _iter_column_sql

django/db/backends/base/schema.py:315–380  ·  view source on GitHub ↗
(
        self, column_db_type, params, model, field, field_db_params, include_default
    )

Source from the content-addressed store, hash-verified

313 # Field <-> database mapping functions
314
315 def _iter_column_sql(
316 self, column_db_type, params, model, field, field_db_params, include_default
317 ):
318 yield column_db_type
319 if collation := field_db_params.get("collation"):
320 yield self._collate_sql(collation)
321 # Work out nullability.
322 null = field.null
323 # Add database default.
324 if field.has_db_default():
325 default_sql, default_params = self.db_default_sql(field)
326 yield f"DEFAULT {default_sql}"
327 params.extend(default_params)
328 include_default = False
329 # Include a default value, if requested.
330 include_default = (
331 include_default
332 and not self.skip_default(field)
333 and
334 # Don't include a default value if it's a nullable field and the
335 # default cannot be dropped in the ALTER COLUMN statement (e.g.
336 # MySQL longtext and longblob).
337 not (null and self.skip_default_on_alter(field))
338 )
339 if include_default:
340 default_value = self.effective_default(field)
341 if default_value is not None:
342 column_default = "DEFAULT " + self._column_default_sql(field)
343 if self.connection.features.requires_literal_defaults:
344 # Some databases can't take defaults as a parameter
345 # (Oracle, SQLite). If this is the case, the individual
346 # schema backend should implement prepare_default().
347 yield column_default % self.prepare_default(default_value)
348 else:
349 yield column_default
350 params.append(default_value)
351 # Oracle treats the empty string ('') as null, so coerce the null
352 # option whenever '' is a possible value.
353 if (
354 field.empty_strings_allowed
355 and not field.primary_key
356 and self.connection.features.interprets_empty_strings_as_nulls
357 ):
358 null = True
359 if field.generated:
360 generated_sql, generated_params = self._column_generated_sql(field)
361 params.extend(generated_params)
362 yield generated_sql
363 elif not null:
364 yield "NOT NULL"
365 elif not self.connection.features.implied_column_null:
366 yield "NULL"
367 if field.primary_key:
368 yield "PRIMARY KEY"
369 elif field.unique:
370 yield "UNIQUE"
371 # Optionally add the tablespace if it's an implicitly indexed column.
372 tablespace = field.db_tablespace or model._meta.db_tablespace

Callers 1

column_sqlMethod · 0.95

Calls 14

_collate_sqlMethod · 0.95
db_default_sqlMethod · 0.95
skip_defaultMethod · 0.95
skip_default_on_alterMethod · 0.95
effective_defaultMethod · 0.95
_column_default_sqlMethod · 0.95
prepare_defaultMethod · 0.95
_column_generated_sqlMethod · 0.95
_comment_sqlMethod · 0.95
has_db_defaultMethod · 0.80
extendMethod · 0.80
getMethod · 0.45

Tested by

no test coverage detected