如何在 Django 中使用 call_command() 在特定数据库上进行 运行 迁移?
How to run migrations on specific database using call_command() in Django?
我只是想知道在运行时使用 management.call_command()
函数调用 $ python manage.py migrate app_name --database db_name
的正确语法是什么。
到目前为止,我有以下内容:
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% 确定我可能错误地调用了 --
参数?谁能用这个给我指出正确的方向?
动态应用的迁移如下:
# 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')
我只是想知道在运行时使用 management.call_command()
函数调用 $ python manage.py migrate app_name --database db_name
的正确语法是什么。
到目前为止,我有以下内容:
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% 确定我可能错误地调用了 --
参数?谁能用这个给我指出正确的方向?
动态应用的迁移如下:
# 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')