When you enter the following command on the terminal to perform data migration:

python manage.py makemigrations
Copy the code

It’s wrong!

/usr/lib/python3/dist-packages/requests/__init__.py:80: RequestsDependencyWarning: urllib3 (1.26.4) or chardet (3.0.4) doesn't match a supported version! RequestsDependencyWarning) Traceback (most recent call last): File "manage.py", line 22, in 
      
        main() File "manage.py", line 18, in main execute_from_command_line(sys.argv) File "/ usr/local/lib/python3.6 / dist - packages/django/core/management/set py", line 401, in execute_from_command_line utility.execute() File "/ usr/local/lib/python3.6 / dist - packages/django/core/management/set py", line 395, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/ usr/local/lib/python3.6 / dist - packages/django/core/management/base. Py", line 330, in run_from_argv self. The execute (* args, * * cmd_options) File "/ usr/local/lib/python3.6 / dist - packages/django/core/management/base. Py", line 371, in execute output = self.handle(*args, * * options) File "/ usr/local/lib/python3.6 / dist - packages/django/core/management/base. Py", line 85, in wrapped res = handle_func(*args, * * kwargs) File "/ usr/local/lib/python3.6 / dist - packages/django/core/management/commands/makemigrations py", line 87, in handle loader = MigrationLoader(None, Ignore_no_migrations = True) File "/ usr/local/lib/python3.6 / dist - packages/django/db/migrations/loader. Py", line 53, In __init__ self. Build_graph () the File "/ usr/local/lib/python3.6 / dist - packages/django/db/migrations/loader. Py", line 255, in build_graph self.graph.validate_consistency() File "/ usr/local/lib/python3.6 / dist - packages/django/db/migrations/graph. Py", line 195, in validate_consistency [n.raise_error() for n in self.node_map.values() if isinstance(n, DummyNode)] File "/ usr/local/lib/python3.6 / dist - packages/django/db/migrations/graph. Py", line 195, in 
       
         [n.raise_error() for n in self.node_map.values() if isinstance(n, DummyNode)] File "/ usr/local/lib/python3.6 / dist - packages/django/db/migrations/graph. Py", line 58, in raise_error raise NodeNotFoundError(self.error_message, self.key, origin=self.origin) django.db.migrations.exceptions.NodeNotFoundError: Migration comments.0001_initial dependencies reference nonexistent parent node ('
       
      tweets', '0002_auto_20210426_0742')

Copy the code

Django doesn’t provide a command to delete migrated files 🤡, and deleting them manually would be boring and a waste of time! I have provided a script written in Python to delete the migration file, put it in your project root directory and execute it!

import os

for root, dirs, files in os.walk('. ') :if 'migrations' in dirs:
        dir = dirs[dirs.index('migrations')]
        for root_j, dirs_j, files_j in os.walk(os.path.join(root, dir)) :for file_k in files_j:
                iffile_k ! ='__init__.py':
                    dst_file = os.path.join(root_j, file_k)
                    print('> > >', dst_file)
                    os.remove(dst_file)

Copy the code

Walk recursively through all folders in the project root path. Find all migrations folders and delete all files except __init__.py. Why not delete the __init__.py file? Since it is not a Python package without the __init__.py file, the Models under this app will not be migrated when the migration command is executed

One idea is to find all migrations folders, delete them and recreate them in their original locations, creating an empty file named __init__.

Why choose a Python script to do this? Python’s cross-platform feature, of course, makes it available on both UNIX and Windows.

Of course, the above code is very ugly 🤡, who is free to optimize after sending me a section of it!