Django 数据库迁移不起作用,未创建新 table
Django database migration not working, new table not created
我最初在 mysql 中创建了一个数据库,并使用 inspectdb 创建了所有模型。到目前为止一切顺利,但后来我开始尝试对模型进行更改并迁移它们。我添加列无济于事,即使迁移文件夹显示更改,数据库中也没有更改。我开始怀疑这是否与大小写有关,所以我再次尝试使用第二个模型 TestTwoT,名称为 table,所有字段均为小写。然后我 运行 生成此输出的 make 迁移:
Migrations for 'testdb':
testdb/migrations/0010_testtwot.py
- Create model TestTwoT
migrations文件夹中生成的.py文件(我觉得没问题):
# Generated by Django 3.0.1 on 2020-02-10 22:46
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('testdb', '0009_auto_20200210_2241'),
]
operations = [
migrations.CreateModel(
name='TestTwoT',
fields=[
('id', models.AutoField(db_column='id', primary_key=True, serialize=False)),
('name', models.CharField(blank=True, db_column='name', max_length=1000, null=True)),
],
options={
'db_table': 'test_two_t',
'managed': True,
},
),
]
然后我 运行 生成此输出的迁移脚本表明创建 table:
没有问题
Operations to perform:
Apply all migrations: admin, auth, contenttypes, testdb, sessions
Running migrations:
Applying testdb.0010_testtwot... OK
但是,当我 运行 'show tables' sql 命令或 'describe test_two_t' 命令时,仍然没有我的新 table 的迹象。
在我的场景中还有另一个复杂之处,即我没有使用默认数据库,但我的设置似乎完全没问题,而且我的应用程序在指向数据库时工作正常。
编辑:按照 Borut 的建议,我将命令更改为(添加 --database 参数):
python3 manage.py makemigrations
python3 manage.py migrate --database=testdb
EDIT2:现在一切正常(在我还删除了所有以前的迁移 .py 文件并从头开始使用更正的上述命令之后)。
根据 Borut 的建议,我将命令更改为(添加 --database 参数):
python3 manage.py makemigrations
python3 manage.py migrate --database=testdb
现在一切正常(在我还删除了所有以前的迁移 .py 文件并从头开始使用更正的上述命令之后)。
我最初在 mysql 中创建了一个数据库,并使用 inspectdb 创建了所有模型。到目前为止一切顺利,但后来我开始尝试对模型进行更改并迁移它们。我添加列无济于事,即使迁移文件夹显示更改,数据库中也没有更改。我开始怀疑这是否与大小写有关,所以我再次尝试使用第二个模型 TestTwoT,名称为 table,所有字段均为小写。然后我 运行 生成此输出的 make 迁移:
Migrations for 'testdb':
testdb/migrations/0010_testtwot.py
- Create model TestTwoT
migrations文件夹中生成的.py文件(我觉得没问题):
# Generated by Django 3.0.1 on 2020-02-10 22:46
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('testdb', '0009_auto_20200210_2241'),
]
operations = [
migrations.CreateModel(
name='TestTwoT',
fields=[
('id', models.AutoField(db_column='id', primary_key=True, serialize=False)),
('name', models.CharField(blank=True, db_column='name', max_length=1000, null=True)),
],
options={
'db_table': 'test_two_t',
'managed': True,
},
),
]
然后我 运行 生成此输出的迁移脚本表明创建 table:
没有问题Operations to perform:
Apply all migrations: admin, auth, contenttypes, testdb, sessions
Running migrations:
Applying testdb.0010_testtwot... OK
但是,当我 运行 'show tables' sql 命令或 'describe test_two_t' 命令时,仍然没有我的新 table 的迹象。
在我的场景中还有另一个复杂之处,即我没有使用默认数据库,但我的设置似乎完全没问题,而且我的应用程序在指向数据库时工作正常。
编辑:按照 Borut 的建议,我将命令更改为(添加 --database 参数):
python3 manage.py makemigrations
python3 manage.py migrate --database=testdb
EDIT2:现在一切正常(在我还删除了所有以前的迁移 .py 文件并从头开始使用更正的上述命令之后)。
根据 Borut 的建议,我将命令更改为(添加 --database 参数):
python3 manage.py makemigrations
python3 manage.py migrate --database=testdb
现在一切正常(在我还删除了所有以前的迁移 .py 文件并从头开始使用更正的上述命令之后)。