django.db.migrations.exceptions.InconsistentMigrationHistory

后端 未结 25 1030
耶瑟儿~
耶瑟儿~ 2020-12-04 08:16

When I run python manage.py migrate on my Django project, I get the following error:

Traceback (most recent call last):
File \"manage.py\", line         


        
相关标签:
25条回答
  • 2020-12-04 09:06

    when you create a new project and with no apps, you run the

    python manage.py migrate
    

    the Django will create 10 tables by default.

    If you want create a customer user model which inherit from AbstractUser after that, you will encounter this problem as follow message:

    django.db.migrations.exceptions.InconsistentMigrationHistory: Migration admin.0001_initial is applied before its dependency account.0001_initial on database 'default'.

    finally, I drop my entire databases and run

    0 讨论(0)
  • 2020-12-04 09:07

    I have to drop my database to and then run makemigrations and migrate again for this to be resolved on my part.

    0 讨论(0)
  • 2020-12-04 09:08

    Okay, before you do anything weird or nuclear, first just drop your database and rebuild it.

    If using POsgres -

    DROP SCHEMA public CASCADE;
    CREATE SCHEMA PUBLIC;
    

    Then just remake your migrations

    ./manage.py migrate
    

    This is the most basic solution, which typically will clear things up. Don't just go remaking the migrations until absolutely neccessary.

    0 讨论(0)
  • 2020-12-04 09:09

    First delete all the migrations and db.sqlite3 files and follow these steps:

    $ ./manage.py makemigrations myapp 
    $ ./manage.py squashmigrations myapp 0001(may be differ)
    

    Delete the old migration file and finally.

    $ ./manage.py migrate
    
    0 讨论(0)
  • 2020-12-04 09:09

    If that exception was reveal itself while you are trying to create your own User model instead of standard follow that instruction
    I have found my problem resolve by follow that instruction step by step:

    1. Create a custom user model identical to auth.User, call it User (so many-to-many tables keep the same name) and set db_table='auth_user' (so it uses the same table)
    2. Throw away all your migrations
    3. Recreate a fresh set of migrations
    4. Sacrifice a chicken, perhaps two if you're anxious; also make a backup of your database
    5. Truncate the django_migrations table
    6. Fake-apply the new set of migrations
    7. Unset db_table, make other changes to the custom model, generate migrations, apply them

    It is highly recommended to do this on a database that enforces foreign key constraints. Don't try this on SQLite on your laptop and expect it to work on Postgres on the servers!

    0 讨论(0)
  • 2020-12-04 09:10

    django.db.migrations.exceptions.InconsistentMigrationHistory #On Creating Custom User Model

    I had that same issue today, and none of the above solutions worked, then I thought to erase all the data from my local PostgreSQL database using this following command

    -- Drop everything from the PostgreSQL database.
    
    DO $$
    DECLARE
            q TEXT;
            r RECORD;
    BEGIN
            -- triggers
            FOR r IN (SELECT pns.nspname, pc.relname, pt.tgname
                    FROM pg_catalog.pg_trigger pt, pg_catalog.pg_class pc, pg_catalog.pg_namespace pns
                    WHERE pns.oid=pc.relnamespace AND pc.oid=pt.tgrelid
                        AND pns.nspname NOT IN ('information_schema', 'pg_catalog', 'pg_toast')
                        AND pt.tgisinternal=false
                ) LOOP
                    EXECUTE format('DROP TRIGGER %I ON %I.%I;',
                        r.tgname, r.nspname, r.relname);
            END LOOP;
            -- constraints #1: foreign key
            FOR r IN (SELECT pns.nspname, pc.relname, pcon.conname
                    FROM pg_catalog.pg_constraint pcon, pg_catalog.pg_class pc, pg_catalog.pg_namespace pns
                    WHERE pns.oid=pc.relnamespace AND pc.oid=pcon.conrelid
                        AND pns.nspname NOT IN ('information_schema', 'pg_catalog', 'pg_toast')
                        AND pcon.contype='f'
                ) LOOP
                    EXECUTE format('ALTER TABLE ONLY %I.%I DROP CONSTRAINT %I;',
                        r.nspname, r.relname, r.conname);
            END LOOP;
            -- constraints #2: the rest
            FOR r IN (SELECT pns.nspname, pc.relname, pcon.conname
                    FROM pg_catalog.pg_constraint pcon, pg_catalog.pg_class pc, pg_catalog.pg_namespace pns
                    WHERE pns.oid=pc.relnamespace AND pc.oid=pcon.conrelid
                        AND pns.nspname NOT IN ('information_schema', 'pg_catalog', 'pg_toast')
                        AND pcon.contype<>'f'
                ) LOOP
                    EXECUTE format('ALTER TABLE ONLY %I.%I DROP CONSTRAINT %I;',
                        r.nspname, r.relname, r.conname);
            END LOOP;
            -- indicēs
            FOR r IN (SELECT pns.nspname, pc.relname
                    FROM pg_catalog.pg_class pc, pg_catalog.pg_namespace pns
                    WHERE pns.oid=pc.relnamespace
                        AND pns.nspname NOT IN ('information_schema', 'pg_catalog', 'pg_toast')
                        AND pc.relkind='i'
                ) LOOP
                    EXECUTE format('DROP INDEX %I.%I;',
                        r.nspname, r.relname);
            END LOOP;
            -- normal and materialised views
            FOR r IN (SELECT pns.nspname, pc.relname
                    FROM pg_catalog.pg_class pc, pg_catalog.pg_namespace pns
                    WHERE pns.oid=pc.relnamespace
                        AND pns.nspname NOT IN ('information_schema', 'pg_catalog', 'pg_toast')
                        AND pc.relkind IN ('v', 'm')
                ) LOOP
                    EXECUTE format('DROP VIEW %I.%I;',
                        r.nspname, r.relname);
            END LOOP;
            -- tables
            FOR r IN (SELECT pns.nspname, pc.relname
                    FROM pg_catalog.pg_class pc, pg_catalog.pg_namespace pns
                    WHERE pns.oid=pc.relnamespace
                        AND pns.nspname NOT IN ('information_schema', 'pg_catalog', 'pg_toast')
                        AND pc.relkind='r'
                ) LOOP
                    EXECUTE format('DROP TABLE %I.%I;',
                        r.nspname, r.relname);
            END LOOP;
            -- sequences
            FOR r IN (SELECT pns.nspname, pc.relname
                    FROM pg_catalog.pg_class pc, pg_catalog.pg_namespace pns
                    WHERE pns.oid=pc.relnamespace
                        AND pns.nspname NOT IN ('information_schema', 'pg_catalog', 'pg_toast')
                        AND pc.relkind='S'
                ) LOOP
                    EXECUTE format('DROP SEQUENCE %I.%I;',
                        r.nspname, r.relname);
            END LOOP;
            -- extensions (only if necessary; keep them normally)
            FOR r IN (SELECT pns.nspname, pe.extname
                    FROM pg_catalog.pg_extension pe, pg_catalog.pg_namespace pns
                    WHERE pns.oid=pe.extnamespace
                        AND pns.nspname NOT IN ('information_schema', 'pg_catalog', 'pg_toast')
                ) LOOP
                    EXECUTE format('DROP EXTENSION %I;', r.extname);
            END LOOP;
            -- aggregate functions first (because they depend on other functions)
            FOR r IN (SELECT pns.nspname, pp.proname, pp.oid
                    FROM pg_catalog.pg_proc pp, pg_catalog.pg_namespace pns, pg_catalog.pg_aggregate pagg
                    WHERE pns.oid=pp.pronamespace
                        AND pns.nspname NOT IN ('information_schema', 'pg_catalog', 'pg_toast')
                        AND pagg.aggfnoid=pp.oid
                ) LOOP
                    EXECUTE format('DROP AGGREGATE %I.%I(%s);',
                        r.nspname, r.proname,
                        pg_get_function_identity_arguments(r.oid));
            END LOOP;
            -- routines (functions, aggregate functions, procedures, window functions)
            IF EXISTS (SELECT * FROM pg_catalog.pg_attribute
                    WHERE attrelid='pg_catalog.pg_proc'::regclass
                        AND attname='prokind' -- PostgreSQL 11+
                ) THEN
                    q := 'CASE pp.prokind
                            WHEN ''p'' THEN ''PROCEDURE''
                            WHEN ''a'' THEN ''AGGREGATE''
                            ELSE ''FUNCTION''
                        END';
            ELSIF EXISTS (SELECT * FROM pg_catalog.pg_attribute
                    WHERE attrelid='pg_catalog.pg_proc'::regclass
                        AND attname='proisagg' -- PostgreSQL ≤10
                ) THEN
                    q := 'CASE pp.proisagg
                            WHEN true THEN ''AGGREGATE''
                            ELSE ''FUNCTION''
                        END';
            ELSE
                    q := '''FUNCTION''';
            END IF;
            FOR r IN EXECUTE 'SELECT pns.nspname, pp.proname, pp.oid, ' || q || ' AS pt
                    FROM pg_catalog.pg_proc pp, pg_catalog.pg_namespace pns
                    WHERE pns.oid=pp.pronamespace
                        AND pns.nspname NOT IN (''information_schema'', ''pg_catalog'', ''pg_toast'')
                ' LOOP
                    EXECUTE format('DROP %s %I.%I(%s);', r.pt,
                        r.nspname, r.proname,
                        pg_get_function_identity_arguments(r.oid));
            END LOOP;
            -- nōn-default schemata we own; assume to be run by a not-superuser
            FOR r IN (SELECT pns.nspname
                    FROM pg_catalog.pg_namespace pns, pg_catalog.pg_roles pr
                    WHERE pr.oid=pns.nspowner
                        AND pns.nspname NOT IN ('information_schema', 'pg_catalog', 'pg_toast', 'public')
                        AND pr.rolname=current_user
                ) LOOP
                    EXECUTE format('DROP SCHEMA %I;', r.nspname);
            END LOOP;
            -- voilà
            RAISE NOTICE 'Database cleared!';
    END; $$;
    

    After this you can run django command for migrations

    python manage.py makemigrations
    python manage.py migrate
    

    And Absolutely that will work . Thank You.

    0 讨论(0)
提交回复
热议问题