我向模型添加了一个新字段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),
),
]
答案 0 :(得分:1)
您的案件可能有两个常见问题。您是伪造了迁移(您未将其应用到数据库),还是正在查看错误的数据库。
首先检查django是否已连接到其他数据库。最快的方法是运行shell:
python manage.py shell
并尝试创建一个带有段子字段的对象。
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
然后再次迁移该弹头
python manage.py migrate book
应该可以。