gpt4 book ai didi

django - 如何在 Django 中使用 call_command() 在特定数据库上运行迁移?

转载 作者:行者123 更新时间:2023-12-04 15:22:44 25 4
gpt4 key购买 nike

我只是想知道调用 $ python manage.py migrate app_name --database db_name 的正确语法是什么与 management.call_command()运行时的功能。
到目前为止,我有以下几点:

from django.core import management
from django.core.management.commands import migrate

# Migrate the core.contrib.dynamics if needed to the pre-specified database:
management.call_command(migrate.Command(), 'dynamics', '--database {}'.format(DB_NAME))
但是,在运行时调用上述方法时出现以下错误:
Cannot find a migration matching '--database default_node' from app 'dynamics'.
我 99% 确定我可能正在调用 -- args 不正确?任何人都可以指出我正确的方向吗?
动态应用程序的迁移如下:
# Generated by Django 3.0.8 on 2020-07-02 14:28

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='ModelSchema',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=32, unique=True)),
('_modified', models.DateTimeField(auto_now=True)),
],
),
migrations.CreateModel(
name='FieldSchema',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=63)),
('data_type', models.CharField(choices=[('character', 'character'), ('text', 'text'), ('integer', 'integer'), ('float', 'float'), ('boolean', 'boolean'), ('date', 'date')], editable=False, max_length=16)),
('null', models.BooleanField(default=False)),
('unique', models.BooleanField(default=False)),
('max_length', models.PositiveIntegerField(null=True)),
('model_schema', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='fields', to='dynamics.ModelSchema')),
],
options={
'unique_together': {('name', 'model_schema')},
},
),
]

最佳答案

考虑到您已经运行了迁移,您可以简单地使用

management.call_command('migrate', app_label='dynamics', database='dbname')

关于django - 如何在 Django 中使用 call_command() 在特定数据库上运行迁移?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62953481/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com