Django makemigrations命令每次都会生成相同的迁移

时间:2019-04-09 09:23:33

标签: django

我的项目基于django框架,makemigrations命令无法正常工作。 makemigrations命令每次都会生成相同的脚本,但是我的代码没有更改。

我升级了Django版本,但问题仍然存在。 django现在是2.2

models.py

class BatchUploadRecord(models.Model):

    create_date = models.DateTimeField(_("date created"), auto_now_add=True)
    update_date = models.DateTimeField(_("date updated"), auto_now=True)


    course = models.ForeignKey(Course, related_name='course_batchtemp', on_delete=models.SET_NULL, null=True)

    lesson = models.ForeignKey(Lesson, related_name='lesson_batchtemp', on_delete=models.SET_NULL, null=True)

移民

class Migration(migrations.Migration):

    dependencies = [
        ('courses', '0025_auto_20190130_1634'),
    ]

    operations = [
        migrations.AlterField(
            model_name='batchuploadrecord',
            name='lesson',
            field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='lesson_batchtemp', to='courses.Lesson'),
        ),
    ]

运行makemigrations命令

(rinpoche) ➜  rinpoche_ask_api git:(develop) ✗ ./manage.py makemigrations 
Migrations for 'courses':
  applications/courses/migrations/0027_auto_20190409_1658.py
    - Alter field lesson on batchuploadrecord
(rinpoche) ➜  rinpoche_ask_api git:(develop) ✗ ./manage.py makemigrations 
Migrations for 'courses':
  applications/courses/migrations/0028_auto_20190409_1658.py
    - Alter field lesson on batchuploadrecord
(rinpoche) ➜  rinpoche_ask_api git:(develop) ✗ ./manage.py makemigrations 
Migrations for 'courses':
  applications/courses/migrations/0029_auto_20190409_1658.py
    - Alter field lesson on batchuploadrecord

我没有解决方法。

1 个答案:

答案 0 :(得分:0)

如果您现在不担心数据库,请尝试删除所有迁移并将其重置。为此,请按照我的指示进行操作:

  1. find . -path "*/migrations/*.py" -not -name "__init__.py" -delete

  2. find . -path "*/migrations/*.pyc" -delete

  3. 删除当前数据库,或删除db.sqlite3
  4. 创建初始迁移并生成数据库模式。

    python manage.py makemigrations

    python manage.py migrate

如果您被困在这里,则是整个教程:https://simpleisbetterthancomplex.com/tutorial/2016/07/26/how-to-reset-migrations.html