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
| Attribute | Type | Description |
|---|
| sql_add_geometry_column | string = "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_index | string = "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_index | string = "DROP TABLE idx_%(table)s_%(column)s" | SQL template used to drop a spatial index table associated with a geometry column. |
| sql_recover_geometry_metadata | string = "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_metadata | string = "SELECT DiscardGeometryColumn(%(table)s, %(column)s)" | SQL template used to remove geometry metadata for a specific column using the DiscardGeometryColumn function. |
| sql_discard_geometry_columns | string = "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_columns | string = "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_tables | list = ["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
| Name | Type | Description |
|---|
| *args | any | Variable length argument list passed to the parent DatabaseSchemaEditor constructor. |
| **kwargs | any | Arbitrary 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
| Name | Type | Description |
|---|
| name | string | The table or column name to be quoted |
Returns
| Type | Description |
|---|
string | The 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
| Name | Type | Description |
|---|
| 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_default | boolean = False | Whether to include the default value in the generated SQL |
Returns
| Type | Description |
|---|
tuple | A tuple of (None, None) for geometry fields to prevent standard column creation, or the standard SQL for non-geometry fields |
@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
| Name | Type | Description |
|---|
| 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
| Name | Type | Description |
|---|
| 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
| Name | Type | Description |
|---|
| model | [Model](../../../../../../db/models/base/model.md?sid=django_db_models_base_model) | The model class to remove from the database |
| kwargs | dict | Additional 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
| Name | Type | Description |
|---|
| 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
| Name | Type | Description |
|---|
| 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
| Name | Type | Description |
|---|
| model | [Model](../../../../../../db/models/base/model.md?sid=django_db_models_base_model) | The model whose table is being renamed |
| old_db_table | string | The current name of the table in the database |
| new_db_table | string | The new name for the table in the database |