Skip to main content

SpatialiteSchemaEditor

This class provides specialized database schema management for SpatiaLite, extending standard schema editing capabilities to handle geographic data types. It manages the creation and removal of geometry columns and spatial indexes through specific stored procedures and metadata table updates. The class ensures that spatial metadata remains synchronized during table alterations, field additions, and model deletions.

Attributes

AttributeTypeDescription
sql_add_geometry_columnstring = "SELECT AddGeometryColumn(%(table)s, %(column)s, %(srid)s, %(geom_type)s, %(dim)s, %(null)s)"SQL template used to create a new geometry column using the SpatiaLite AddGeometryColumn function.
sql_add_spatial_indexstring = "SELECT CreateSpatialIndex(%(table)s, %(column)s)"SQL template used to create a spatial index for a geometry column using the CreateSpatialIndex function.
sql_drop_spatial_indexstring = "DROP TABLE idx_%(table)s_%(column)s"SQL template used to drop a spatial index table associated with a geometry column.
sql_recover_geometry_metadatastring = "SELECT RecoverGeometryColumn(%(table)s, %(column)s, %(srid)s, %(geom_type)s, %(dim)s)"SQL template used to restore geometry metadata for a table using the RecoverGeometryColumn function.
sql_remove_geometry_metadatastring = "SELECT DiscardGeometryColumn(%(table)s, %(column)s)"SQL template used to remove geometry metadata for a specific column using the DiscardGeometryColumn function.
sql_discard_geometry_columnsstring = "DELETE FROM %(geom_table)s WHERE f_table_name = %(table)s"SQL template used to delete geometry metadata entries from a specific geometry table for a given database table.
sql_update_geometry_columnsstring = "UPDATE %(geom_table)s SET f_table_name = %(new_table)s WHERE f_table_name = %(old_table)s"SQL template used to update the table name in geometry metadata tables during a table rename operation.
geometry_tableslist = ["geometry_columns", "geometry_columns_auth", "geometry_columns_time", "geometry_columns_statistics"]A list of SpatiaLite system table names that store geometry metadata and statistics.

Constructor

Signature

def SpatialiteSchemaEditor(
*args: any,
**kwargs: any
) - > null

Parameters

NameTypeDescription
*argsanyVariable length argument list passed to the parent DatabaseSchemaEditor constructor.
**kwargsanyArbitrary keyword arguments passed to the parent DatabaseSchemaEditor constructor.

Methods


geo_quote_name()

@classmethod
def geo_quote_name(
name: string
) - > string

Quotes a database identifier specifically for SpatiaLite geometry functions.

Parameters

NameTypeDescription
namestringThe table or column name to be quoted

Returns

TypeDescription
stringThe quoted identifier string suitable for use in spatial SQL queries

column_sql()

@classmethod
def column_sql(
model: [Model](../../../../../../db/models/base/model.md?sid=django_db_models_base_model),
field: [Field](../../../../../../forms/fields/field.md?sid=django_forms_fields_field),
include_default: boolean = False
) - > tuple

Generates the SQL for a column and queues geometry-specific creation commands if the field is a GeometryField.

Parameters

NameTypeDescription
model[Model](../../../../../../db/models/base/model.md?sid=django_db_models_base_model)The Django model class the field belongs to
field[Field](../../../../../../forms/fields/field.md?sid=django_forms_fields_field)The field instance for which to generate SQL
include_defaultboolean = FalseWhether to include the default value in the generated SQL

Returns

TypeDescription
tupleA tuple of (None, None) for geometry fields to prevent standard column creation, or the standard SQL for non-geometry fields

remove_geometry_metadata()

@classmethod
def remove_geometry_metadata(
model: [Model](../../../../../../db/models/base/model.md?sid=django_db_models_base_model),
field: [GeometryField](../../../../forms/fields/geometryfield.md?sid=django_contrib_gis_forms_fields_geometryfield)
)

Removes spatial metadata and drops the spatial index associated with a specific geometry field.

Parameters

NameTypeDescription
model[Model](../../../../../../db/models/base/model.md?sid=django_db_models_base_model)The model containing the geometry field
field[GeometryField](../../../../forms/fields/geometryfield.md?sid=django_contrib_gis_forms_fields_geometryfield)The geometry field whose metadata and index should be purged

create_model()

@classmethod
def create_model(
model: [Model](../../../../../../db/models/base/model.md?sid=django_db_models_base_model)
)

Creates the database table for a model and executes any queued geometry column creation and spatial indexing SQL.

Parameters

NameTypeDescription
model[Model](../../../../../../db/models/base/model.md?sid=django_db_models_base_model)The model class to create in the database

delete_model()

@classmethod
def delete_model(
model: [Model](../../../../../../db/models/base/model.md?sid=django_db_models_base_model),
kwargs: dict
)

Deletes the database table and ensures all associated spatial metadata and geometry column records are purged.

Parameters

NameTypeDescription
model[Model](../../../../../../db/models/base/model.md?sid=django_db_models_base_model)The model class to remove from the database
kwargsdictAdditional keyword arguments passed to the base delete_model method

add_field()

@classmethod
def add_field(
model: [Model](../../../../../../db/models/base/model.md?sid=django_db_models_base_model),
field: [Field](../../../../../../forms/fields/field.md?sid=django_forms_fields_field)
)

Adds a new field to a table, using SpatiaLite stored procedures if the field is a geometry type.

Parameters

NameTypeDescription
model[Model](../../../../../../db/models/base/model.md?sid=django_db_models_base_model)The model class to which the field is being added
field[Field](../../../../../../forms/fields/field.md?sid=django_forms_fields_field)The field instance to add to the database table

remove_field()

@classmethod
def remove_field(
model: [Model](../../../../../../db/models/base/model.md?sid=django_db_models_base_model),
field: [Field](../../../../../../forms/fields/field.md?sid=django_forms_fields_field)
)

Removes a field from a table, forcing a table recreation if the field is a geometry type to ensure proper metadata cleanup.

Parameters

NameTypeDescription
model[Model](../../../../../../db/models/base/model.md?sid=django_db_models_base_model)The model class from which the field is being removed
field[Field](../../../../../../forms/fields/field.md?sid=django_forms_fields_field)The field instance to remove

alter_db_table()

@classmethod
def alter_db_table(
model: [Model](../../../../../../db/models/base/model.md?sid=django_db_models_base_model),
old_db_table: string,
new_db_table: string
)

Renames a database table and updates all internal SpatiaLite geometry metadata and spatial index table names.

Parameters

NameTypeDescription
model[Model](../../../../../../db/models/base/model.md?sid=django_db_models_base_model)The model whose table is being renamed
old_db_tablestringThe current name of the table in the database
new_db_tablestringThe new name for the table in the database