SchemaEditor

class BaseDatabaseSchemaEditor[sumber]

Django's migration system is split into two parts; the logic for calculating and storing what operations should be run (django.db.migrations), and the database abstraction layer that turns things like "create a model" or "delete a field" into SQL - which is the job of the SchemaEditor.

It's unlikely that you will want to interact directly with SchemaEditor as a normal developer using Django, but if you want to write your own migration system, or have more advanced needs, it's a lot nicer than writing SQL.

Each database backend in Django supplies its own version of SchemaEditor, and it's always accessible via the connection.schema_editor() context manager:

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

It must be used via the context manager as this allows it to manage things like transactions and deferred SQL (like creating ForeignKey constraints).

It exposes all possible operations as methods, that should be called in the order you wish changes to be applied. Some possible operations or types of change are not possible on all databases - for example, MyISAM does not support foreign key constraints.

Jika anda sedng menulis atau merawat backend basisdata pihak-ketiga untuk Django, anda akan butuh menyediakan penerapan SchemaEditor untuk bekerja dengan kegunaan perpindahan 1.7 - bagaimanapun, selama basisdata anda masih standar dalam penggunaannya dari SQL dan rancangan hubungan, anda harus dapat mensubkelaskan satu dari kelas SchemaEditor Django siap-pakai dan cukup mengutik sintaksis sedikit. Juga catat bahwa ada sedikit fitur basisdata baru yang perpindahan akan mencari: can_rollback_ddl dan supports_combined_alters adalah paling penting.

Cara

execute()

BaseDatabaseSchemaEditor.execute(sql, params=[])[sumber]

Executes the SQL statement passed in, with parameters if supplied. This is a simple wrapper around the normal database cursors that allows capture of the SQL to a .sql file if the user wishes.

create_model()

BaseDatabaseSchemaEditor.create_model(model)[sumber]

Membuat sebuah tabel baru dalam basisdata untuk model yang disediakan, bersama dengan batasan unik apapun atau indeks-indeks dia butuhkan.

delete_model()

BaseDatabaseSchemaEditor.delete_model(model)[sumber]

Jatuhkan tabel model dalam basisdata bersama dengan batasan unik apapun atau indeks itu punya.

add_index()

BaseDatabaseSchemaEditor.add_index(model, index)[sumber]

Tambah index ke tabel model.

remove_index()

BaseDatabaseSchemaEditor.remove_index(model, index)[sumber]

Pindah index ke tabel model.

add_constraint()

BaseDatabaseSchemaEditor.add_constraint(model, constraint)[sumber]
New in Django 2.2.

Adds constraint to model's table.

remove_constraint()

BaseDatabaseSchemaEditor.remove_constraint(model, constraint)[sumber]
New in Django 2.2.

Removes constraint from model's table.

alter_unique_together()

BaseDatabaseSchemaEditor.alter_unique_together(model, old_unique_together, new_unique_together)[sumber]

Changes a model's unique_together value; this will add or remove unique constraints from the model's table until they match the new value.

alter_index_together()

BaseDatabaseSchemaEditor.alter_index_together(model, old_index_together, new_index_together)[sumber]

Changes a model's index_together value; this will add or remove indexes from the model's table until they match the new value.

alter_db_table()

BaseDatabaseSchemaEditor.alter_db_table(model, old_db_table, new_db_table)[sumber]

Renames the model's table from old_db_table to new_db_table.

alter_db_tablespace()

BaseDatabaseSchemaEditor.alter_db_tablespace(model, old_db_tablespace, new_db_tablespace)[sumber]

Memindahkan tabel model dari sat tablespace ke lainnya.

add_field()

BaseDatabaseSchemaEditor.add_field(model, field)[sumber]

Adds a column (or sometimes multiple) to the model's table to represent the field. This will also add indexes or a unique constraint if the field has db_index=True or unique=True.

If the field is a ManyToManyField without a value for through, instead of creating a column, it will make a table to represent the relationship. If through is provided, it is a no-op.

If the field is a ForeignKey, this will also add the foreign key constraint to the column.

remove_field()

BaseDatabaseSchemaEditor.remove_field(model, field)[sumber]

Removes the column(s) representing the field from the model's table, along with any unique constraints, foreign key constraints, or indexes caused by that field.

If the field is a ManyToManyField without a value for through, it will remove the table created to track the relationship. If through is provided, it is a no-op.

alter_field()

BaseDatabaseSchemaEditor.alter_field(model, old_field, new_field, strict=False)[sumber]

This transforms the field on the model from the old field to the new one. This includes changing the name of the column (the db_column attribute), changing the type of the field (if the field class changes), changing the NULL status of the field, adding or removing field-only unique constraints and indexes, changing primary key, and changing the destination of ForeignKey constraints.

The most common transformation this cannot do is transforming a ManyToManyField into a normal Field or vice-versa; Django cannot do this without losing data, and so it will refuse to do it. Instead, remove_field() and add_field() should be called separately.

If the database has the supports_combined_alters, Django will try and do as many of these in a single database call as possible; otherwise, it will issue a separate ALTER statement for each change, but will not issue ALTERs where no change is required (as South often did).

Atribut

Semua atribut harus dianggap hanya-baca meskipun dinyatakan sebaliknya.

connection

SchemaEditor.connection

A connection object to the database. A useful attribute of the connection is alias which can be used to determine the name of the database being accessed.

Ini berguna ketika melakukan perpindahan data untuk migrations with multiple databases.