【问题标题】:Django - a migration was detected, but saying no migrations to applyDjango - 检测到迁移,但说没有要应用的迁移
【发布时间】:2018-08-31 05:13:22
【问题描述】:

我向模型添加了一个新字段 slug = models.SlugField(unique=True, null=True)。已成功检测到新的迁移。

(venv) 192-168-1-201:shop jinx$ python manage.py makemigrations book
Migrations for 'book':
  book/migrations/0014_book_slug.py
    - Add field slug to book

但是当我尝试应用迁移时,它说:

(venv) 192-168-1-201:shop jinx$ python manage.py migrate book
Operations to perform:
  Apply all migrations: book
Running migrations:
  No migrations to apply.

表未更新,未添加新列。 而且 --fake 也无济于事。

(venv) 192-168-1-201:shop jinx$ python manage.py migrate --fake book
Operations to perform:
  Apply all migrations: book
Running migrations:
  No migrations to apply.

我已经多次遇到这个问题,但每次我都以某种方式解决了这个问题。现在我想知道是什么导致了这个问题?

模型中的字段:

class Book(models.Model):

    title = models.CharField(max_length=191, unique=True)

    slug = models.SlugField(unique=True, null=True)  # new field 

    pub_date = models.DateField()

    publisher = models.CharField(max_length=191)

    language = models.CharField(max_length=191)

    print_length = models.DecimalField(max_digits=6, decimal_places=0)

    price = models.DecimalField(max_digits=10, decimal_places=2)

    authors = models.ManyToManyField(Author, related_name='written_by')

    categories = models.ManyToManyField(Category)

    quantity = models.IntegerField(default=0)

    description = models.TextField(null=True)

    photo = models.ImageField(upload_to='book photos', null=True,blank=True)

    is_active = models.BooleanField(default=True)

    created_date = models.DateTimeField(auto_now_add=True, editable=False)

    last_modified = models.DateTimeField(auto_now=True)

显示迁移:

book
 [X] 0001_initial
 [X] 0002_auto_20180819_2147
 [X] 0003_auto_20180819_2147
 [X] 0004_auto_20180819_2148
 [X] 0005_auto_20180819_2151
 [X] 0006_auto_20180819_2152
 [X] 0007_auto_20180819_2154
 [X] 0008_auto_20180819_2155
 [X] 0009_auto_20180819_2159
 [X] 0010_auto_20180819_2201
 [X] 0011_book_photo
 [X] 0012_auto_20180826_1502
 [X] 0013_auto_20180828_2214
 [X] 0014_book_slug

表格状态:

mysql> describe book_book;
+---------------+---------------+------+-----+---------+----------------+
| Field         | Type          | Null | Key | Default | Extra          |
+---------------+---------------+------+-----+---------+----------------+
| id            | int(11)       | NO   | PRI | NULL    | auto_increment |
| title         | varchar(255)  | NO   |     | NULL    |                |
| pub_date      | date          | NO   |     | NULL    |                |
| publisher     | varchar(255)  | NO   |     | NULL    |                |
| language      | varchar(255)  | NO   |     | NULL    |                |
| print_length  | decimal(6,0)  | NO   |     | NULL    |                |
| price         | decimal(10,2) | NO   |     | NULL    |                |
| quantity      | int(11)       | YES  |     | NULL    |                |
| description   | longtext      | YES  |     | NULL    |                |
| created_date  | datetime(6)   | NO   |     | NULL    |                |
| is_active     | tinyint(1)    | NO   |     | NULL    |                |
| last_modified | datetime(6)   | NO   |     | NULL    |                |
| photo         | varchar(100)  | YES  |     | NULL    |                |
+---------------+---------------+------+-----+---------+----------------+
13 rows in set (0.01 sec)

0014_book_slug.py:

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('book', '0013_auto_20180828_2214'),
    ]

    operations = [
        migrations.AddField(
            model_name='book',
            name='slug',
            field=models.SlugField(null=True, unique=True),
        ),
    ]

【问题讨论】:

  • 你能把python manage.py showmigrations的结果加起来吗?
  • @JPG 当然可以。完成
  • 你为什么要运行--fake?这告诉 django 将其标记为已应用,但什么也不做。如果你想运行迁移,只需使用python manage.py migrate book
  • 我不知道为什么会这样。一种解决方案是,如果您不在生产环境中,请删除数据库并重新创建一个新数据库
  • @Jinx : 你能执行“select * from django_migrations”并检查表中是否存在“0014_book_slug”条目吗?

标签: django


【解决方案1】:

有两个常见问题可能适用于您的案例。要么你伪造了迁移(你没有将它应用到数据库),要么你正在查看错误的数据库。

首先检查 django 是否连接到不同的数据库。最快的方法是运行shell:

python manage.py shell

并尝试使用 slug 字段创建一个对象。

from apps.books.models import Book
Book.objects.create(
    slug="something"
    # Enter all other r
)

如果它有效,则设置了 slug 字段,并且您可能已连接到另一个已经迁移它的数据库。如果它通知您 slug 不存在,则您可能伪造了迁移。

如果您以某种方式伪造了迁移,则应回滚到迁移 0013。 (编辑:添加--fake)

python manage.py migrate book 0013 --fake

然后再次迁移 slug

python manage.py migrate book

应该可以的。

【讨论】:

  • 我很确定我正在对正确的数据库/表执行此操作。当我运行python manage.py migrate 0013' 时,它会显示Can't DROP 'slug'; check that column/key exists。如果我执行Book.objects.fitler(slug='something') 之类的操作,它会显示Unknown column 'book_book.slug' in 'field list'
  • 尝试“python manage.py migrate book 0013 --fake”,我忘了在答案中写--fake。
  • 成功了!谢谢你。你可以编辑你的答案然后我接受并投票吗?
猜你喜欢
  • 1970-01-01
  • 2020-10-14
  • 2019-06-23
  • 2021-07-20
  • 2017-06-24
  • 2016-04-14
  • 2018-10-24
  • 2014-02-11
  • 2018-03-07
相关资源
最近更新 更多