Schema API

class django_spanner.schema.DatabaseSchemaEditor(connection, collect_sql=False, atomic=True)[source]

The database abstraction layer that turns things like “create a model” or “delete a field” into SQL.

add_constraint(model, constraint)

Add a constraint to a model.

add_field(model, field)[source]

Add a column (or sometimes multiple) to the model’s table to represent the field. This will also add indexes or a unique constraint if the field has db_index=True or unique=True. If the field is a ManyToManyField without a value for through, instead of creating a column, it will make a table to represent the relationship. If through is provided, it is a no-op. If the field is a ForeignKey, this will also add the foreign key constraint to the column.

Parameters
  • model (ModelOperation) – A model for creating a table.

  • field (FieldOperation) – The field of the table.

add_index(model, index)[source]

Add index to model’s table.

Parameters
  • model (ModelOperation) – A model for creating a table.

  • index (Index) – An index to add.

alter_db_table(model, old_db_table, new_db_table)

Rename the table a model points to.

alter_db_tablespace(model, old_db_tablespace, new_db_tablespace)

Move a model’s table between tablespaces.

alter_field(model, old_field, new_field, strict=False)

Allow a field’s type, uniqueness, nullability, default, column, constraints, etc. to be modified. old_field is required to compute the necessary changes. If strict is True, raise errors if the old column does not match old_field precisely.

alter_index_together(model, old_index_together, new_index_together)

Deal with a model changing its index_together. The input index_togethers must be doubly-nested, not the single-nested [“foo”, “bar”] format.

alter_unique_together(model, old_unique_together, new_unique_together)

Deal with a model changing its unique_together. The input unique_togethers must be doubly-nested, not the single-nested [“foo”, “bar”] format.

column_sql(model, field, include_default=False, exclude_not_null=False)[source]

Take a field and return its column definition. The field must already have had set_attributes_from_name() called.

Parameters
  • model (ModelOperation) – A model for creating a table.

  • field (FieldOperation) – The field of the table.

  • include_default (bool) – (Optional) Flag for including default fields.

  • exclude_not_null (bool) – (Optional) Flag for excluding not null fields.

create_model(model)[source]

Create a table and any accompanying indexes or unique constraints for the given model.

Parameters

model (ModelOperation) – A model for creating a table.

delete_model(model)[source]

Drop the model’s table in the database along with any unique constraints or indexes it has.

Parameters

model (ModelOperation) – A model for creating a table.

effective_default(field)

Return a field’s effective database default value.

execute(sql, params=())

Execute the given SQL statement, with optional parameters.

prepare_default(value)

Only used for backends which have requires_literal_defaults feature

quote_value(value)[source]

Return a quoted version of the value so it’s safe to use in an SQL string. This is not safe against injection from user code; it is intended only for use in making SQL scripts or preparing default values for particularly tricky backends (defaults are not user-defined, though, so this is safe).

remove_constraint(model, constraint)

Remove a constraint from a model.

remove_field(model, field)[source]

Remove the column(s) representing the field from the model’s table, along with any unique constraints, foreign key constraints, or indexes caused by that field. If the field is a ManyToManyField without a value for through, it will remove the table created to track the relationship. If through is provided, it is a no-op.

Parameters
  • model (ModelOperation) – A model for creating a table.

  • field (FieldOperation) – The field of the table.

remove_index(model, index)

Remove an index from a model.

skip_default(field)[source]

Cloud Spanner doesn’t support column defaults.

table_sql(model)

Take a model and return its table definition.