92

South 已经有类似的问题,但我已经使用 Django 1.7 开始了我的项目,并且没有使用 South。

在开发过程中,已经创建了很多迁移,但是该软件尚未交付,并且不存在必须迁移的数据库。因此,我想重置迁移,就好像我当前的模型是原始模型一样,并重新创建所有数据库。

推荐的方法是什么?

编辑:从 Django 1.8 开始,有一个名为squashmigrations的新命令或多或少地解决了这里描述的问题。

4

11 回答 11

139

我懂了。我刚刚想通了,这很好。

  • 首先,清除迁移表:

    ./manage.py migrate --fake <app-name> zero
    
  • 删除app-name/migrations/文件夹或内容。

  • 进行迁移:

    ./manage.py makemigrations <app-name>
    
  • 最后在不进行其他数据库更改的情况下整理您的迁移:

    ./manage.py migrate --fake <app-name>
    
于 2014-11-19T03:35:30.020 回答
36

在 Django 1.7 版本的迁移中,过去在 South 中的重置功能已被删除,取而代之的是用于“压缩”迁移的新功能。这应该是控制迁移数量的好方法。

https://docs.djangoproject.com/en/dev/topics/migrations/#squashing-migrations

如果您仍然想真正从头开始,我假设您仍然可以通过清空迁移表并删除迁移,之后您将makemigrations再次运行。

于 2014-05-20T09:17:00.817 回答
22

我只是有同样的问题。这是我的解决方法。

#!/bin/sh
echo "Starting ..."

echo ">> Deleting old migrations"
find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc"  -delete


# Optional
echo ">> Deleting database"
find . -name "db.sqlite3" -delete

echo ">> Running manage.py makemigrations"
python manage.py makemigrations

echo ">> Running manage.py migrate"
python manage.py migrate

echo ">> Done"

find命令:http ://unixhelp.ed.ac.uk/CGI/man-cgi ? find

于 2015-01-30T10:51:49.173 回答
7

假设这是您的项目结构,

project_root/
    app1/
        migrations/
    app2/
        migrations/
    ...
    manage.py
    remove_migrations.py

您可以从上面指示的位置运行脚本 remove_migrations.py 以删除所有迁移文件。

#remove_migrations.py
"""
Run this file from a Django =1.7 project root. 
Removes all migration files from all apps in a project.
""" 
from unipath import Path

this_file = Path(__file__).absolute()
current_dir = this_file.parent
dir_list = current_dir.listdir()

for paths in dir_list:
    migration_folder = paths.child('migrations')
    if migration_folder.exists():
        list_files = migration_folder.listdir()
        for files in list_files:
            split = files.components()
            if split[-1] != Path('__init__.py'):
                files.remove()

如果您有一个复杂的项目,手动删除可能会很累。这为我节省了很多时间。删除迁移文件是安全的。我已经这样做了无数次而没有遇到任何问题……但是。

但是,当我删除了迁移文件夹,makemigrations或者migrate没有为我创建该文件夹时。该脚本确保迁移文件夹及其__init__.py保留,仅删除迁移文件。

于 2014-09-25T08:56:50.623 回答
6
  1. 删除文件:delete_migrations.py(在 prj 的根目录中):
import os

for root, dirs, files in os.walk(".", topdown=False):
  for name in files:
      if '/migrations' in root and name != '__init__.py':
          os.remove(os.path.join(root, name))
  1. DELETE FROM django_migrations Where app in ('app1', 'app2');

  2. ./manage.py makemigrations

  3. ./manage.py 迁移 --fake

或者,您可以从这一切编写迁移

于 2016-03-01T08:35:48.340 回答
4

我尝试了不同的命令,其中一些答案对我有帮助。在我的例子中,只有这个序列修复了 MYAPP 中迁移中损坏的依赖关系,并从头开始清理所有过去的迁移。

在执行此操作之前,请确保数据库已经同步(例如,不要在此处添加新的模型字段或更改元选项)。

rm -Rf MYAPP/migrations/*
python manage.py makemigrations --empty MYAPP
python manage.py makemigrations
python manage.py migrate --fake MYAPP 0002

其中 0002 是最后一个 makemigrations 命令返回的迁移编号。

现在您可以再次正常运行 makemigrations / migrate 因为迁移 0002 已存储但未反映在已同步的数据库中。

于 2015-12-06T09:29:22.923 回答
3

如果您不关心以前的迁移,那么仅删除 migrations/ 目录中的所有迁移怎么样?您将从头开始迁移序列,以您当前的模型作为参考,就好像您现在已经编写了整个模型一样。

如果您不相信我可以删除,请尝试将它们移走。

于 2014-09-04T19:55:49.513 回答
1

一个简单的方法是

转到每个应用程序并删除迁移文件。

然后转到数据库中的 django-migrtaions 表并截断它(删除所有条目)。

之后,您可以再次创建迁移。

于 2016-09-06T11:36:15.853 回答
0

cd 到 src 目录 cd /path/to/src

删除迁移目录 rm -rf your_app/migrations/

请注意,这应该分别为每个应用程序完成

迁移 python3.3 manage.py migrate

如果你想重新开始 python3.3 manage.py makemigrations your_app

于 2014-11-18T15:44:42.160 回答
0

如果您处于开发模式并且只想重置所有内容(数据库、迁移等),我会根据 Abdelhamid Ba 的回答使用此脚本。这将擦除数据库(Postgres)的表,删除所有迁移文件,重新运行迁移并加载我的初始固定装置:

#!/usr/bin/env bash
echo "This will wipe out the database, delete migration files, make and apply migrations and load the intial fixtures."

while true; do
    read -p "Do you wish to continue?" yn
    case $yn in
        [Yy]* ) make install; break;;
        [Nn]* ) exit;;
        * ) echo "Please answer yes or no.";;
    esac
done

echo ">> Deleting old migrations"
find ../../src -path "*/migrations/*.py" -not -name "__init__.py" -delete

# Optional
echo ">> Deleting database"
psql -U db_user -d db_name -a -f ./reset-db.sql

echo ">> Running manage.py makemigrations and migrate"
./migrations.sh

echo ">> Loading initial fixtures"
./load_initial_fixtures.sh

echo ">> Done"

重置-db.sql 文件:

DO $$ DECLARE
    r RECORD;
BEGIN
    -- if the schema you operate on is not "current", you will want to
    -- replace current_schema() in query with 'schematodeletetablesfrom'
    -- *and* update the generate 'DROP...' accordingly.
    FOR r IN (SELECT tablename FROM pg_tables WHERE schemaname = current_schema()) LOOP
        EXECUTE 'DROP TABLE IF EXISTS ' || quote_ident(r.tablename) || ' CASCADE';
    END LOOP;
END $$;

迁移.sh 文件:

#!/usr/bin/env bash
cd ../../src
./manage.py makemigrations
./manage.py migrate

load_initial_fixtures.sh 文件:

#!/usr/bin/env bash
cd ../../src
./manage.py loaddata ~/path-to-fixture/fixture.json

请务必将路径更改为与您的应用程序相对应。我个人将这些脚本放在一个名为 project_root/script/local 的文件夹中,而 django 的源代码在 project_root/src 中。

于 2016-06-24T07:01:35.253 回答
0

在我的应用程序中(手动)删除每个“迁移”文件夹后,我运行:

./manage.py dbshell
delete from django_migrations;

然后我想我可以做到./manage.py makemigrations让它们全部再生。但是,没有检测到任何变化。然后我尝试一次指定一个应用程序:./manage.py makemigrations foo, ./manage.py makemigrations bar. 但是,这导致无法解决的循环依赖关系。

最后,我运行了一个 makemigrations 命令,该命令指定了我的所有应用程序(无特定顺序):

./manage.py makemigrations foo bar bike orange banana etc

这一次,它起作用了——循环依赖被自动解决(它在必要时创建了额外的迁移文件)。

然后我能够跑步./manage.py migrate --fake并重新开始工作。

于 2016-11-04T18:23:16.000 回答