Schema Manager

  • class SchemaManager(model[, database=None[, **context_options]])

Parameters:

  • model (Model) – Model class.
  • database (Database) – If unspecified defaults to model._meta.database.

Provides methods for managing the creation and deletion of tables andindexes for the given model.

  • createtable([_safe=True[, **options]])

Parameters:

  1. - **safe** (_bool_) Specify IF NOT EXISTS clause.
  2. - **options** Arbitrary options.

Execute CREATE TABLE query for the given model.

  • droptable([_safe=True[, drop_sequences=True[, **options]]])

Parameters:

  1. - **safe** (_bool_) Specify IF EXISTS clause.
  2. - **drop_sequences** (_bool_) Drop any sequences associated with thecolumns on the table (postgres only).
  3. - **options** Arbitrary options.

Execute DROP TABLE query for the given model.

  • truncatetable([_restart_identity=False[, cascade=False]])

Parameters:

  1. - **restart_identity** (_bool_) Restart the id sequence (postgres-only).
  2. - **cascade** (_bool_) Truncate related tables as well (postgres-only).

Execute TRUNCATE TABLE for the given model. If the database is Sqlite,which does not support TRUNCATE, then an equivalent DELETE query willbe executed.

  • createindexes([_safe=True])

Parameters:safe (bool) – Specify IF NOT EXISTS clause.

Execute CREATE INDEX queries for the indexes defined for the model.

  • dropindexes([_safe=True])

Parameters:safe (bool) – Specify IF EXISTS clause.

Execute DROP INDEX queries for the indexes defined for the model.

  • createsequence(_field)

Parameters:field (Field) – Field instance which specifies a sequence.

Create sequence for the given Field.

  • dropsequence(_field)

Parameters:field (Field) – Field instance which specifies a sequence.

Drop sequence for the given Field.

  • createforeign_key(_field)

Parameters:field (ForeignKeyField) – Foreign-key field constraint to add.

Add a foreign-key constraint for the given field. This method shouldnot be necessary in most cases, as foreign-key constraints are createdas part of table creation. The exception is when you are creating acircular foreign-key relationship using DeferredForeignKey.In those cases, it is necessary to first create the tables, then addthe constraint for the deferred foreign-key:

  1. class Language(Model):
  2. name = TextField()
  3. selected_snippet = DeferredForeignKey('Snippet')
  4.  
  5. class Snippet(Model):
  6. code = TextField()
  7. language = ForeignKeyField(Language, backref='snippets')
  8.  
  9. # Creates both tables but does not create the constraint for the
  10. # Language.selected_snippet foreign key (because of the circular
  11. # dependency).
  12. db.create_tables([Language, Snippet])
  13.  
  14. # Explicitly create the constraint:
  15. Language._schema.create_foreign_key(Language.selected_snippet)

For more information, see documentation on Circular foreign key dependencies.

Warning

Because SQLite has limited support for altering existing tables, itis not possible to add a foreign-key constraint to an existingSQLite table.

  • createall([_safe=True[, **table_options]])

Parameters:safe (bool) – Whether to specify IF NOT EXISTS.

Create sequence(s), index(es) and table for the model.

  • dropall([_safe=True[, drop_sequences=True[, **options]]])

Parameters:

  1. - **safe** (_bool_) Whether to specify IF EXISTS.
  2. - **drop_sequences** (_bool_) Drop any sequences associated with thecolumns on the table (postgres only).
  3. - **options** Arbitrary options.

Drop table for the model and associated indexes.