存在迁移时,Django 迁移不会运行新的迁移

问题描述

我正在努力将对外关系添加到现有模型中。我创建了没有问题的外部模型并将外键实现到我现有的模型中。我运行了创建迁移文件makemigrations。但是,当我运行 migrate 时,没有应用迁移,我收到消息说没有新的迁移可以应用 - 即使有一个

我不太确定此时还可以尝试什么。我已经删除了我新创建的迁移,并在迁移之前尝试重新运行 makemigrations,但这根本没有帮助。我仔细检查以确保迁移文件具有所有正确的信息,并且基于之前的迁移。我还确保我在项目目录中的正确位置,我的虚拟环境正在运行并且 docker 正在运行。我还尝试在运行迁移时指定迁移文件及其所在的应用程序,但这也不起作用。

我是 docker 新手,所以我不确定这是否是导致问题的原因。我查看了 this stackoverflow 帖子,我认为这可能会有所帮助,但看了之后,我真的不知道这是否解决了我的问题。我可以运行 python makemigrations 没有错误,只是不能python migrate链接的帖子似乎适用于两者都不起作用的情况。

根据下面显示错误消息,我怀疑发生的事情是迁移从未应用于不存在的调度程序应用程序数据库中的外键(经验)列。因此,当我尝试在本地计算机上的 helm 上查看 Scheduler 时,它会因尝试引用不存在的数据库列而出错。

尝试在 helm 上查看调度程序模型时出现的错误消息:

enter image description here

现有模型的 python showmigrations 结果:

enter image description here

在调度器中创建迁移后 python manage.py migrate 的结果:

enter image description here

代码段:

调度器模型.py

from django.db import models
from user.models import UserProfile
from museum.models import Simulator,Museum
from schedule.models import Event
from experience.models import Experience

# Create your models here.
class ScheduleEvent(Event):
    pilot = models.ForeignKey(
        UserProfile,on_delete=models.CASCADE,name="pilot",null=True,blank=True,related_name="pilot_event",)
    copilot = models.ForeignKey(
        UserProfile,name="copilot",related_name="copilot_event",)
    experience = models.ForeignKey(Experience,name="experience",blank=True)
    simulator = models.ForeignKey(Simulator,on_delete=models.CASCADE)
    is_claimed = models.BooleanField(verbose_name="Event is claimed and confirmed",name="is_claimed",default=False)

我迁移到调度程序应用程序: 0004_scheduleevent_experience.py

# Generated by Django 2.2.5 on 2021-02-23 15:49

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


class Migration(migrations.Migration):

    dependencies = [
        ('experience','0001_initial'),('scheduler','0003_auto_20201111_1822'),]

    operations = [
        migrations.AddField(
            model_name='scheduleevent',name='experience',field=models.ForeignKey(blank=True,on_delete=django.db.models.deletion.CASCADE,to='experience.Experience'),),]

体验应用模型.py

from django.db import models

class Experience(models.Model):
    type = models.CharField(max_length = 10,null=False,blank=False)
    description = models.TextField(max_length=100,blank=True)
    time = models.DateTimeField()
    cost = models.DecimalField(max_digits=6,decimal_places=2)

体验应用中的最新迁移

# Generated by Django 2.2.5 on 2021-02-08 17:40

from django.db import migrations,models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Experience',fields=[
                ('id',models.AutoField(auto_created=True,primary_key=True,serialize=False,verbose_name='ID')),('type',models.CharField(max_length=10)),('description',models.TextField(blank=True,max_length=100)),('time',models.DateTimeField()),('cost',models.DecimalField(decimal_places=2,max_digits=6)),],]

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)