django(新增model)No migrations to apply.

django 1.8版本,在models下新建一个class,无法在数据库创建新表的问题:

- models.py

class HostPwd(models.Model):
    hostname = models.CharField(max_length=32, unique=True)
    username = models.CharField(max_length=24, null=True, blank=True)
    password = models.CharField(max_length=24, null=True, blank=True)
    desc = models.TextField(max_length=32, blank=True, null=True)

-新增 0001_initial.py 文件之后:

migrations.CreateModel(
            name='HostPwd',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('hostname', models.CharField(unique=True, max_length=32)),
                ('username', models.CharField(null=True, blank=True, max_length=24)),
                ('password', models.CharField(null=True, blank=True, max_length=24)),
                ('desc', models.TextField(null=True, blank=True, max_length=32)),

            ],
        ),

解决办法:

第一步:删除该app下migrations 下的__init__.py 文件

第二步:进入数据库,找到django_migrations的表,删除该app名字的所有记录。


第三步:执行下面这两条命令:(在项目目录下)

python manage.py makemigrations
python manage.py migrate

原因:

django_migrations表记录着数据库的对应表的修改记录。
每次修改后,都执行第三步的命令,然后在第一步的文件夹下生成修改的文件,django_migrations表记录修改的变更过程。
原文地址:https://www.cnblogs.com/able7/p/8809586.html