Schema Manager
Parameters:
Provides methods for managing the creation and deletion of tables andindexes for the given model.
Parameters:
- **safe** (_bool_) – Specify IF NOT EXISTS clause.
- **options** – Arbitrary options.
Execute CREATE TABLE query for the given model.
Parameters:
- **safe** (_bool_) – Specify IF EXISTS clause.
- **drop_sequences** (_bool_) – Drop any sequences associated with thecolumns on the table (postgres only).
- **options** – Arbitrary options.
Execute DROP TABLE query for the given model.
Parameters:
- **restart_identity** (_bool_) – Restart the id sequence (postgres-only).
- **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.
Parameters:safe (bool) – Specify IF NOT EXISTS clause.
Execute CREATE INDEX queries for the indexes defined for the model.
Parameters:safe (bool) – Specify IF EXISTS clause.
Execute DROP INDEX queries for the indexes defined for the model.
Parameters:field (Field) – Field instance which specifies a sequence.
Create sequence for the given Field
.
Parameters:field (Field) – Field instance which specifies a sequence.
Drop sequence for the given 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:
- class Language(Model):
- name = TextField()
- selected_snippet = DeferredForeignKey('Snippet')
- class Snippet(Model):
- code = TextField()
- language = ForeignKeyField(Language, backref='snippets')
- # Creates both tables but does not create the constraint for the
- # Language.selected_snippet foreign key (because of the circular
- # dependency).
- db.create_tables([Language, Snippet])
- # Explicitly create the constraint:
- 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.
Parameters:safe (bool) – Whether to specify IF NOT EXISTS.
Create sequence(s), index(es) and table for the model.
Parameters:
- **safe** (_bool_) – Whether to specify IF EXISTS.
- **drop_sequences** (_bool_) – Drop any sequences associated with thecolumns on the table (postgres only).
- **options** – Arbitrary options.
Drop table for the model and associated indexes.