我正在使用Django 2.0,Python
每次我在创建新模型时
python3 manage.py makemigrations
显示no changes detected
和迁移no migrations to apply
即使我尝试了
python3 manage.py makemigrations home
显示
Migrations for 'home':
home/migrations/0001_initial.py
- Create model FAQ
- Create model Topic
然后
python3 manage.py migrate home
它仍然显示
Operations to perform:
Apply all migrations: home
Running migrations:
No migrations to apply.
当我尝试在admin中打开该表时。
显示
OperationalError at /admin/home/faq/
no such table: home_faq
然而,当我删除迁移和数据库并重新迁移所有内容时它会起作用。
但我不能每次都删除所有内容。
my models.py
from django.db import models
# Create your models here.
class Topic(models.Model):
topic_title = models.CharField(max_length=200,unique=True)
topic_discription = models.TextField()
no_of_posts = models.IntegerField()
def __str__(self):
return self.topic_title
class FAQ(models.Model):
question = models.CharField(max_length=800,unique=True)
answer = models.TextField()
timestamp = models.DateTimeField(auto_now_add=True,auto_now=False)
author = models.CharField(max_length=100)
author_info = models.TextField()
def __str__(self):
return self.question
我已在admin.py
上注册了它from django.contrib import admin
from .models import Topic,FAQ
# Register your models here.
admin.site.register(Topic)
admin.site.register(FAQ)
编辑#1:
好的,现在我清除了所有迁移和db.sqlite3并运行了
# python3 manage.py makemigrations
No changes detected
# python3 manage.py makemigrations home
Migrations for 'home':
home/migrations/0001_initial.py
- Create model FAQ
- Create model Topic
# python3 manage.py migrate home
Operations to perform:
Apply all migrations: home
Running migrations:
Applying home.0001_initial... OK
它确实有效但我为此重建了数据库。
因为它尚未投入生产,所以没关系,但以后可能会成为大问题。
要注意的事情是
python3 manage.py makemigrations
显示
No changes detected
但是在
时创建了迁移python3 manage.py makemigrations home
并且对
有友好的回应python3 manage.py migrate home
所以,即使有任何方法强制重建db中的模型,也可以。(python3 manage.py 强制迁移)
^可能是解决方案。如果存在 force
编辑#2:
已安装的应用:
INSTALLED_APPS = [
'home.apps.HomeConfig',
'UserProfile.apps.UserprofileConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]