Object representation of a single index. Supports equality and inequality comparison of IndexSchema.
name is the column name. parent is an instance of TableSchema
Note
IndexSchema objects are automatically created for you by IndexSchemaBuilder and loaded under schema.databases[name].tables[name].indexes
Example
>>> schema.databases['sakila'].tables['rental'].indexes.keys()
['PRIMARY', 'rental_date', 'idx_fk_inventory_id', 'idx_fk_customer_id', 'idx_fk_staff_id']
Index Attributes
>>> schema.databases['sakila'].tables['rental'].indexes['idx_fk_customer_id'].name
'idx_fk_customer_id'
>>> schema.databases['sakila'].tables['rental'].indexes['idx_fk_customer_id'].table_name
'rental'
>>> schema.databases['sakila'].tables['rental'].indexes['idx_fk_customer_id'].non_unique
True
>>> schema.databases['sakila'].tables['rental'].indexes['idx_fk_customer_id'].fields
[('customer_id', None)]
#possible types: BTREE, RTREE, HASH
>>> schema.databases['sakila'].tables['rental'].indexes['idx_fk_customer_id'].type
'BTREE'
#possible kinds: PRIMARY, UNIQUE, FULLTEXT, SPATIAL, INDEX
>>> schema.databases['sakila'].tables['rental'].indexes['rental_date'].kind
'UNIQUE'
>>> schema.databases['sakila'].tables['film_text'].indexes['idx_title_description'].kind
'FULLTEXT'
#fields is a list of tuples (field_name, sub_part_length)
>>> schema.databases['sakila'].tables['film_text'].indexes['idx_title_description'].fields
[('title', 0), ('description', 0)]
#collation will always be A in MySQL 5.x - 6.x
>>> schema.databases['sakila'].tables['rental'].indexes['idx_fk_customer_id'].collation
'A'
>>> schema.databases['sakila'].tables['rental'].indexes['idx_fk_customer_id'].comment
''
Generate the SQL to create (ADD) this index
>>> schema.databases['sakila'].tables['film_text'].indexes['idx_title_description'].create()
'ADD FULLTEXT INDEX `idx_title_description` (`title`, `description`)'
>>> schema.databases['sakila'].tables['rental'].indexes['rental_date'].create()
'ADD UNIQUE INDEX `rental_date` (`rental_date`, `inventory_id`, `customer_id`) USING BTREE'
Generate the SQL to drop this index
>>> schema.databases['sakila'].tables['rental'].indexes['PRIMARY'].drop()
'DROP PRIMARY KEY'
>>> schema.databases['sakila'].tables['rental'].indexes['rental_date'].drop()
'DROP INDEX `rental_date`'
Generate the SQL to format the sub_part length of an indexed column name
>>> schemaobjects.index.IndexSchema.format_sub_part('column', 0)
'`column`'
>>> schemaobjects.index.IndexSchema.format_sub_part('column', 5)
'`column`(5)'
Returns a dictionary loaded with all of the indexes available in the table. table must be an instance of TableSchema.
Note
This function is automatically called for you and set to schema.databases[name].tables[name].indexes when you create an instance of SchemaObject