SchemaEditor

class BaseDatabaseSchemaEditor[源代码]

Django的迁移系统分为两部分:计算和存储应运行哪些操作的逻辑 (django.db.migrations )和将“创建模型”或“删除字段”等内容转换为SQL的数据库抽象层-这是 SchemaEditor .

您不太可能希望直接与 SchemaEditor 作为一个使用django的普通开发人员,但是如果您想编写自己的迁移系统,或者有更高级的需求,那么它比编写SQL要好得多。

Django中的每个数据库后端都提供自己的版本 SchemaEditor ,并且始终可以通过 connection.schema_editor() 上下文管理器:

with connection.schema_editor() as schema_editor:
    schema_editor.delete_model(MyModel)

它必须通过上下文管理器使用,因为这允许它管理事务和延迟的SQL(如创建 ForeignKey 约束条件)。

它将所有可能的操作公开为方法,这些方法应按希望应用更改的顺序调用。某些可能的操作或更改类型不可能在所有数据库上进行-例如,myisam不支持外键约束。

如果您正在为Django编写或维护第三方数据库后端,则需要提供 SchemaEditor 实现以便使用Django的迁移功能-但是,只要您的数据库在使用SQL和关系设计方面是相对标准的,您就应该能够子类化其中一个内置的Django SchemaEditor 类,并稍微调整一下语法。

方法

execute()

BaseDatabaseSchemaEditor.execute(sql, params=())[源代码]

执行传入的SQL语句(如果提供了参数)。这是普通数据库游标的包装器,它允许将SQL捕获到 .sql 如果用户愿意,请归档。

create_model()

BaseDatabaseSchemaEditor.create_model(model)[源代码]

在数据库中为所提供的模型创建一个新表,以及它所需的任何唯一约束或索引。

delete_model()

BaseDatabaseSchemaEditor.delete_model(model)[源代码]

删除数据库中模型的表以及它所具有的任何唯一约束或索引。

add_index()

BaseDatabaseSchemaEditor.add_index(model, index)[源代码]

添加 indexmodel 的表格。

remove_index()

BaseDatabaseSchemaEditor.remove_index(model, index)[源代码]

移除 indexmodel 的表格。

rename_index()

BaseDatabaseSchemaEditor.rename_index(model, old_index, new_index)[源代码]

重命名 old_index 从… model 的表到 new_index

add_constraint()

BaseDatabaseSchemaEditor.add_constraint(model, constraint)[源代码]

添加 constraintmodel 的桌子。

remove_constraint()

BaseDatabaseSchemaEditor.remove_constraint(model, constraint)[源代码]

移除 constraintmodel 的桌子。

alter_unique_together()

BaseDatabaseSchemaEditor.alter_unique_together(model, old_unique_together, new_unique_together)[源代码]

更改模型的 unique_together 值;这将添加或删除模型表中的唯一约束,直到它们与新值匹配。

alter_index_together()

BaseDatabaseSchemaEditor.alter_index_together(model, old_index_together, new_index_together)[源代码]

更改模型的 index_together 值;这将在模型的表中添加或删除索引,直到它们与新值匹配。

alter_db_table()

BaseDatabaseSchemaEditor.alter_db_table(model, old_db_table, new_db_table)[源代码]

重命名模型的表 old_db_tablenew_db_table .

alter_db_table_comment()

BaseDatabaseSchemaEditor.alter_db_table_comment(model, old_db_table_comment, new_db_table_comment)[源代码]

更改 model 的表格注释到 new_db_table_comment

alter_db_tablespace()

BaseDatabaseSchemaEditor.alter_db_tablespace(model, old_db_tablespace, new_db_tablespace)[源代码]

将模型的表从一个表空间移动到另一个表空间。

add_field()

BaseDatabaseSchemaEditor.add_field(model, field)[源代码]

向模型表中添加一列(有时是多列)以表示字段。如果字段具有 db_index=Trueunique=True .

如果字段是 ManyToManyField 没有值 through ,而不是创建列,而是创建一个表来表示关系。如果 through 提供了,它是不可操作的。

如果字段是 ForeignKey ,这还将向列添加外键约束。

remove_field()

BaseDatabaseSchemaEditor.remove_field(model, field)[源代码]

从模型表中删除表示字段的列,以及由该字段引起的任何唯一约束、外键约束或索引。

如果字段是没有值的ManytomanyField through ,它将删除为跟踪关系而创建的表。如果 through 提供了,它是不可操作的。

alter_field()

BaseDatabaseSchemaEditor.alter_field(model, old_field, new_field, strict=False)[源代码]

这会将模型上的字段从旧字段转换为新字段。这包括更改列的名称 db_column 属性),更改字段类型(如果字段类更改),更改 NULL 字段的状态,仅添加或删除字段的唯一约束和索引,更改主键,以及更改的目标 ForeignKey 约束条件。

最常见的转换是转换 ManyToManyField 进入一个正常字段,或者反之亦然;Django不能在不丢失数据的情况下完成此操作,因此它将拒绝执行此操作。相反, remove_field()add_field() 应该单独调用。

如果数据库具有 supports_combined_alters ,Django将尝试在单个数据库调用中尽可能多地执行这些操作;否则,它将为每个更改发出单独的ALTER语句,但在不需要更改的情况下不会发出ALTER语句。

属性

除非另有说明,否则所有属性都应视为只读。

connection

SchemaEditor.connection

数据库的连接对象。连接的一个有用属性是 alias 可用于确定正在访问的数据库的名称。

这在为执行数据迁移时非常有用 migrations with multiple databases .