generate-migration

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Generate Django Database Migrations

生成Django数据库迁移文件

Commands

命令

Generate migrations automatically based on model changes:
bash
sentry django makemigrations
For a specific app:
bash
sentry django makemigrations <app_name>
Generate an empty migration (for data migrations or custom work):
bash
sentry django makemigrations <app_name> --empty
根据模型变更自动生成迁移文件:
bash
sentry django makemigrations
针对特定应用:
bash
sentry django makemigrations <app_name>
生成空迁移文件(用于数据迁移或自定义操作):
bash
sentry django makemigrations <app_name> --empty

After Generating

生成迁移后

  1. If you added a new model, ensure it's imported in the app's
    __init__.py
  2. Review the generated migration for correctness
  3. Run
    sentry django sqlmigrate <app_name> <migration_name>
    to verify the SQL
  1. 如果添加了新模型,请确保已在应用的
    __init__.py
    中导入该模型
  2. 检查生成的迁移文件是否正确
  3. 运行
    sentry django sqlmigrate <app_name> <migration_name>
    来验证对应的SQL语句

Guidelines

操作指南

Adding Columns

添加列

  • Use
    db_default=<value>
    instead of
    default=<value>
    for columns with defaults
  • Nullable columns: use
    null=True
  • Not null columns: must have
    db_default
    set
  • 对于带默认值的列,使用
    db_default=<value>
    而非
    default=<value>
  • 可空列:设置
    null=True
  • 非空列:必须设置
    db_default

Adding Indexes

添加索引

For large tables, set
is_post_deployment = True
on the migration as index creation may exceed the 5s timeout.
对于大型表,在迁移文件中设置
is_post_deployment = True
,因为创建索引可能会超过5秒超时限制。

Deleting Columns

删除列

  1. Make column nullable (
    null=True
    ) if not already
  2. Remove all code references
  3. Replace
    RemoveField
    with
    SafeRemoveField(..., deletion_action=DeletionAction.MOVE_TO_PENDING)
  4. Deploy, then create second migration with
    SafeRemoveField(..., deletion_action=DeletionAction.DELETE)
  1. 如果列不是可空的,先将其设置为可空(
    null=True
  2. 移除所有代码中对该列的引用
  3. RemoveField
    替换为
    SafeRemoveField(..., deletion_action=DeletionAction.MOVE_TO_PENDING)
  4. 部署后,创建第二个迁移文件,使用
    SafeRemoveField(..., deletion_action=DeletionAction.DELETE)

Deleting Tables

删除表

  1. Remove all code references
  2. Replace
    DeleteModel
    with
    SafeDeleteModel(..., deletion_action=DeletionAction.MOVE_TO_PENDING)
  3. Deploy, then create second migration with
    SafeDeleteModel(..., deletion_action=DeletionAction.DELETE)
  1. 移除所有代码中对该表的引用
  2. DeleteModel
    替换为
    SafeDeleteModel(..., deletion_action=DeletionAction.MOVE_TO_PENDING)
  3. 部署后,创建第二个迁移文件,使用
    SafeDeleteModel(..., deletion_action=DeletionAction.DELETE)

Renaming Columns/Tables

重命名列/表

Don't rename in Postgres. Use
db_column
or
Meta.db_table
to keep the old name.
不要在Postgres中直接重命名。使用
db_column
Meta.db_table
来保留原有名称。

Resolving Merge Conflicts

解决合并冲突

If
migrations_lockfile.txt
conflicts:
bash
bin/update-migration <migration_name>
This renames your migration, updates dependencies, and fixes the lockfile.
如果
migrations_lockfile.txt
出现冲突:
bash
bin/update-migration <migration_name>
该命令会重命名你的迁移文件、更新依赖关系并修复锁文件。