我通过删除一个字段并添加一些字段以及添加一个新表来更新数据库中某个表的某些字段。我跑:
python manage.py makemigrations
python manage.py migrate
而且我没有检测到任何更改,也没有应用任何更改。
在 django admin 中,我没有看到新表,当我单击向数据库中添加新记录时,它会显示旧字段。这也反射(reflect)在数据库中,因为它显示旧字段而不是新表。
我已经删除了数据库中的所有表并删除了每个应用程序中的所有迁移,留下了 init.py,但我仍然收到这个错误,这让我感到困惑,因为它如何显示我已删除和删除的旧字段没有记录因为我删除了表和迁移?
有问题的代码:
from __future__ import unicode_literals
from django.db import models
from src.profiles.models import Profiles
# Create your models here.
class Product(models.Model):
name = models.CharField(max_length=200, primary_key=True)
description = models.CharField(max_length=1000)
price = models.DecimalField(max_digits=10, decimal_places=2)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
class ProductsOwned(models.Model):
ownedID = models.ForeignKey(Profiles.user)
product = models.ForeignKey(Product.name)
purchaseDate = models.DateTimeField(auto_now=True)
expiryDate = models.DateTimeField()
def __str__(self):
return self.name
如果您需要任何说明或有任何疑问,请提出。
编辑:我可以从字面上删除模型中的所有代码,但仍不会检测到任何更改。
编辑 2:将迁移代码上传到 pastebin:http://pastebin.com/GywuiCZj
编辑3:我正在编辑的应用程序是目录,我安装的应用程序:
INSTALLED_APPS = [
'profiles',
'contact',
'crispy_forms',
'django.contrib.sites',
'allauth',
'allauth.account',
'allauth.socialaccount',
'stripe',
'checkout',
'catalog',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
最佳答案
如果您的 catalog
没有任何数据,并且删除与 catalog
应用程序相关的表是安全的,那么您可以执行以下操作。
- 如果您使用的是 MySQL,
DELETE FROM django_migrations where app = 'catalog'
- 从
catalog\migrations
中删除所有迁移。 - 现在执行
python manage.py makemigrations catalog
。
附带说明一下,在 settings.py 中列出应用程序的标准方式是首先列出 django 应用程序,其次是第三方应用程序,然后是您自己的应用程序。
关于python - 在 Django 中,迁移未应用于数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43034343/