Table 在迁移文件中找不到
Table not found in migration file
错误信息
django.db.utils.OperationalError: no such table: clientauth_tbltokentypes
我想做什么
我正在尝试迁移 table 代的数据。
models.py
class tbltokentypes(models.Model):
token_type_id: AutoField(primary_key=True)
token_type: CharField(max_length=30)
我 运行 命令 python manage.py makemigrations
,它创建了文件 0001_initial.py。
然后在迁移文件中,我添加了管理员:
from django.db import migrations, models
from clientauth.models import tbltokentypes
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='tbltokentypes',
fields=[
('token_type_id', models.AutoField(primary_key=True, serialize=False, verbose_name='token_type_id')),
('token_type', models.CharField(max_length=30)),
],
managers=[
tbltokentypes(token_type = "Registration").save()
]
)
]
我错过了什么吗?
将您的函数调用包装在 migrations.RunPython
中,否则在分配 operations
时会 运行,甚至在迁移之前 运行 创建您的 table.
from django.db import migrations, models
# from clientauth.models import tbltokentypes # Remove this
# Add this function
def migrate_tbltokentypes(apps, schema_editor):
# We can't import the Person model directly as it may be a newer
# version than this migration expects. We use the historical version.
tbltokentypes = apps.get_model('clientauth', 'tbltokentypes')
tbltokentypes(token_type="Registration").save()
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='tbltokentypes',
fields=[
('token_type_id', models.AutoField(primary_key=True, serialize=False)),
('token_type', models.CharField(max_length=30)),
],
# managers=[ # Change this
# tbltokentypes(token_type = "Registration").save() #
# ] #
),
migrations.RunPython(migrate_tbltokentypes), # to this
]
另请注意,来自 https://docs.djangoproject.com/en/3.2/topics/migrations/#data-migrations:
[Data migrations are] best written as separate migrations, sitting alongside your schema migrations.
python manage.py makemigrations --empty clientauth --name migrate_tbltokentypes
错误信息
django.db.utils.OperationalError: no such table: clientauth_tbltokentypes
我想做什么
我正在尝试迁移 table 代的数据。
models.py
class tbltokentypes(models.Model):
token_type_id: AutoField(primary_key=True)
token_type: CharField(max_length=30)
我 运行 命令 python manage.py makemigrations
,它创建了文件 0001_initial.py。
然后在迁移文件中,我添加了管理员:
from django.db import migrations, models
from clientauth.models import tbltokentypes
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='tbltokentypes',
fields=[
('token_type_id', models.AutoField(primary_key=True, serialize=False, verbose_name='token_type_id')),
('token_type', models.CharField(max_length=30)),
],
managers=[
tbltokentypes(token_type = "Registration").save()
]
)
]
我错过了什么吗?
将您的函数调用包装在 migrations.RunPython
中,否则在分配 operations
时会 运行,甚至在迁移之前 运行 创建您的 table.
from django.db import migrations, models
# from clientauth.models import tbltokentypes # Remove this
# Add this function
def migrate_tbltokentypes(apps, schema_editor):
# We can't import the Person model directly as it may be a newer
# version than this migration expects. We use the historical version.
tbltokentypes = apps.get_model('clientauth', 'tbltokentypes')
tbltokentypes(token_type="Registration").save()
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='tbltokentypes',
fields=[
('token_type_id', models.AutoField(primary_key=True, serialize=False)),
('token_type', models.CharField(max_length=30)),
],
# managers=[ # Change this
# tbltokentypes(token_type = "Registration").save() #
# ] #
),
migrations.RunPython(migrate_tbltokentypes), # to this
]
另请注意,来自 https://docs.djangoproject.com/en/3.2/topics/migrations/#data-migrations:
[Data migrations are] best written as separate migrations, sitting alongside your schema migrations.
python manage.py makemigrations --empty clientauth --name migrate_tbltokentypes