尝试了很多有关Django迁移问题的方法,但仍然无法解决此错误?

时间:2019-02-28 06:29:08

标签: django django-migrations

大家好,我在过去两天的迁移中遇到了错误。我在模型中添加了一个新字段,然后尝试运行makemigratons,但效果很好,但是当我尝试migrate时,然后我遇到了一个 datetime错误。经过大量的努力,我终于删除了我的数据库。然后,它工作正常,但是当我尝试迁移时,它现在向我显示

return Database.Cursor.execute(self, query, params) django.db.utils.OperationalError: no such table: app_upload

即使我有一个上载模型

here,s the models.py

from django.db import models
from django.contrib.auth.models import User
from django.urls import reverse
from tinymce import HTMLField

from datetime import datetime

class Category(models.Model):
    title = models.CharField(max_length = 20)

    def __str__(self):
        return self.title

class Post(models.Model):
    image = models.ImageField()
    title = models.CharField(max_length = 100)
    body = HTMLField()
    published_date = models.DateTimeField(auto_now_add = True)
    categories = models.ManyToManyField(Category)
    featured = models.BooleanField(default = False)


    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('blog',kwargs = {
            'pk':self.pk
            })

    @property
    def get_comments(self):
        return self.comments.all()




class Comment(models.Model):
    post = models.ForeignKey(Post,on_delete=models.CASCADE,related_name = "comments")
    name = models.CharField(max_length = 200)
    body = models.TextField(default = True)
    parent = models.ForeignKey("self",on_delete = models.CASCADE)
    pub_date = models.DateTimeField(auto_now_add = True)

    class Meta:
        ordering = ['-pub_date']

    def __str__(self):
       return self.name


    def children(self):
        return Comment.objects.filter(parent = self)

    @property
    def is_parent(self):
        if self.parent is not None:
            return False
        return True 


class Upload(models.Model):
    image = models.ImageField(upload_to = 'images',)
    file = models.FileField(upload_to = 'images/%Y/%M/%d/')

这里是创建的迁移

# Generated by Django 2.1.7 on 2019-02-27 13:37

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


class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Category',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('title', models.CharField(max_length=20)),
            ],
        ),
        migrations.CreateModel(
            name='Comment',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(max_length=200)),
                ('body', models.TextField(default=True)),
                ('pub_date', models.DateTimeField(auto_now_add=True)),
                ('parent', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='app.Comment')),
            ],
            options={
                'ordering': ['-pub_date'],
            },
        ),
        migrations.CreateModel(
            name='Post',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('image', models.ImageField(upload_to='')),
                ('title', models.CharField(max_length=100)),
                ('body', tinymce.models.HTMLField()),
                ('published_date', models.DateTimeField(auto_now_add=True)),
                ('featured', models.BooleanField(default=False)),
                ('categories', models.ManyToManyField(to='app.Category')),
            ],
        ),
        migrations.CreateModel(
            name='Upload',
            fields=[
                ('image', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('file', models.DateTimeField(auto_now_add=True)),
            ],
        ),
        migrations.AddField(
            model_name='comment',
            name='post',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='comments', to='app.Post'),
        ),
    ]

在这里是对uploads.py的迁移

由Django 2.1.7于2019-02-28 06:22生成

从django.db导入迁移,模型

类迁移(migrations.Migration):

dependencies = [
    ('app', '0002_auto_20190227_0541'),
]

operations = [
    migrations.AddField(
        model_name='upload',
        name='id',
        field=models.AutoField(auto_created=True, default=1, primary_key=True, serialize=False, verbose_name='ID'),
        preserve_default=False,
    ),
]

2 个答案:

答案 0 :(得分:0)

您已经删除了数据库,因此没有这样的表。

由于已删除数据库,因此您也可以删除迁移文件并重新生成它们。

答案 1 :(得分:0)

如果您不担心数据库中的现有数据,而只想修复迁移并启动并运行

  • 检查您的应用是否在
INSTALLED_APPS = [
    ......
    'my_app',
    .....

]
  • 删除数据库
  • 创建数据库
  • 删除所有迁移文件,但不删除__init__.py
  • python manage.py makemigrations
  • python manage.py migrate