我试图使用makemigrations命令在现有的应用程序中创建迁移,但它输出“未检测到更改”。

通常我使用startapp命令创建新的应用程序,但在创建这个应用程序时没有使用它。

调试后,我发现它没有创建迁移,因为迁移包/文件夹从应用程序中丢失。

如果文件夹不存在或者我遗漏了什么,如果它创建文件夹会更好吗?


当前回答

你能做的最好的事情是,删除现有的数据库。在我的情况下,我使用phpMyAdmin SQL数据库,所以我手动删除创建的数据库在那里。

删除后: 我在PhpMyAdmin创建数据库,不添加任何表。

再次执行以下命令:

Python manage.py makemigrations

Python manage.py迁移

在这些命令之后:你可以看到django已经自动在数据库中创建了其他必要的表(大约有10个表)。

Python manage.py makemigrations <app_name>

Python manage.py迁移

最后:在以上命令之后,您所创建的所有模型(表)都直接导入到数据库中。

希望这能有所帮助。

其他回答

我从django外部复制了一个表,Meta类默认为“managed = false”。例如:

class Rssemailsubscription(models.Model):
    id = models.CharField(primary_key=True, max_length=36)
    ...
    area = models.FloatField('Area (Sq. KM)', null=True)

    class Meta:
        managed = False
        db_table = 'RSSEmailSubscription'

通过将managed改为True, makemig开始发生变化。

更新:在尝试之前,应该确保migrations文件夹中存在__init__.py文件:

./manage.py makemigrations <myapp1> <myapp2>…< myappN >


有时。/manage.py makemigrations优于。/manage.py makemigrations <myapp>,因为它可以处理应用程序之间的某些冲突。

这些情况都是悄无声息地发生的,需要几个小时的咒骂才能理解可怕的“检测到没有变化”消息的真正含义。

因此,使用下面的命令是一个更好的选择:

./manage.py makemigrations <myapp1> <myapp2>…< myappN >

你能做的最好的事情是,删除现有的数据库。在我的情况下,我使用phpMyAdmin SQL数据库,所以我手动删除创建的数据库在那里。

删除后: 我在PhpMyAdmin创建数据库,不添加任何表。

再次执行以下命令:

Python manage.py makemigrations

Python manage.py迁移

在这些命令之后:你可以看到django已经自动在数据库中创建了其他必要的表(大约有10个表)。

Python manage.py makemigrations <app_name>

Python manage.py迁移

最后:在以上命令之后,您所创建的所有模型(表)都直接导入到数据库中。

希望这能有所帮助。

我这样解决了这个问题:

Erase the "db.sqlite3" file. The issue here is that your current data base will be erased, so you will have to remake it again. Inside the migrations folder of your edited app, erase the last updated file. Remember that the first created file is: "0001_initial.py". For example: I made a new class and register it by the "makemigrations" and "migrate" procedure, now a new file called "0002_auto_etc.py" was created; erase it. Go to the "pycache" folder (inside the migrations folder) and erase the file "0002_auto_etc.pyc". Finally, go to the console and use "python manage.py makemigrations" and "python manage.py migrate".

这很简单,你需要在空的migrations文件夹中添加空的init.py。 然后使用"python manage.py makemigrations"检查迁移

目录结构,

你的应用 迁移 init.py