您将如何创建“手动” django迁移? [英] How would you create a 'manual' django migration?

查看:87
本文介绍了您将如何创建“手动” django迁移?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现我可以使用 migrations.RunSQL('some sql')在django项目中为postgres数据库设置列的默认值。

I've discovered that I can set defaults for columns on a postgres database in a django project using migrations.RunSQL('some sql').

我目前正在通过添加列,makemigrations,然后删除列,makemigrations,然后手动修改生成的迁移文件来进行此操作。

I'm currently doing this by adding a column, makemigrations, then removing the column, makemigrations, and then manually modifying the migration file that is produced.

我尝试复制一个旧的迁移文件,然后删除旧的代码,以便只运行新的sql并出现一些奇怪的错误。

I tried copying an old migration file and then removing the old code so just the new sql could be run and got some strange errors.

CommandError: Conflicting migrations detected; multiple leaf nodes in the migration graph: (0067_auto_20180509_2327, 0068_auto_20180514_0707 in csmu).
To fix them run python manage.py makemigrations --merge

如何创建一个'manual'django迁移?

How would you create a 'manual' django migration?

推荐答案

您可以通过运行以下命令来创建手动迁移:

You can create a manual migration by running the command:

python manage.py makemigrations --name migration_name app_ame --empty

app_name 与要添加迁移的项目中的应用相对应。请记住,Django同时管理项目和应用程序(一个项目是特定网站的配置和应用程序的集合。一个项目可以包含多个应用程序。一个应用程序可以位于多个项目中。)
- -empty 标志用于创建一个迁移文件,您将必须在其中添加手动迁移。

Where app_name correspond to the app within your project you want to add the migration. Remember Django manages both Project and Apps (A project is a collection of configuration and apps for a particular website. A project can contain multiple apps. An app can be in multiple projects.) And --empty flag is to create a migration file where you will have to add your manual migrations.

例如 ,在一个名为 api 的应用程序的项目中,该应用程序仅运行一个迁移文件 0001_initial.py : p>

For example, on a project where you have an app named api that only has one migration file 0001_initial.py running:

python manage.py makemigrations api --name migration_example --empty

将在目录 api / migrations / 0002_migration_example.py 的文件。 code>如下所示:

will create a file named 0002_migration_example.py under the directory api/migrations/ that will look like:

# Generated by Django 2.2.10 on 2020-05-26 20:37

from django.db import migrations


class Migration(migrations.Migration):

    dependencies = [
        ('api', '0001_initial'),
    ]

    operations = [
    ]

您应该在操作括号内添加 migrations.RunSQL('some sql')。,例如:

And you should add migrations.RunSQL('some sql'). inside operations brackets, like:

    operations = [
      migrations.RunSQL('some sql')
    ]

这篇关于您将如何创建“手动” django迁移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆