Django Migration is not applying the migration changes

后端 未结 8 690
悲哀的现实
悲哀的现实 2021-02-02 15:41

Using django 1.7.7 I want to use django\'s migration to add or remove a field. so I modified model.py and ran

python manage.py makemigrations myproj
Migrations f         


        
相关标签:
8条回答
  • 2021-02-02 16:16

    Similar to Andrew E above but with a few changes especially where you haven't deleted the migrations folder in your quest to resolve the issue

    1 - In your intact migration folder just examine the 000*.py files counting from the highest down to initial.py till you find the one where your Model is defined, say 0002_entry.py

    2 - python manage.py sqlmigrate app-name 0002 > 0002_sql.txt to capture the SQL commands

    3 - Edit this file to ensure there are no hard CR/LFs and the ALTER, CREATE INDEX commands are each on own single line

    4 - Log into your DB (I have Postgres) and run these commands

    0 讨论(0)
  • 2021-02-02 16:20

    I find Django migrations a bit of a mystery, and tend to prefer external tools (liquibase, for example).

    However, I just ran into this "No migrations to apply" problem as well. I also tried removing the migrations folder, which doesn't help.

    If you've already removed the migrations folder, here's an approach that worked for me.

    First, generate the new "clean" migrations:

    $ python manage.py makemigrations foo
    Migrations for 'foo':
      dashboard/foo/migrations/0001_initial.py
        - Create model Foo
        - Create model Bar
    

    Then look at the SQL and see if it looks reasonable:

    $ python manage.py sqlmigrate foo 0001
    BEGIN;
    --
    -- Create model Foo
    --
    CREATE TABLE "foo" ("id" serial NOT NULL PRIMARY KEY, ... "created_at" timestamp with time zone NOT NULL, "updated_at" timestamp with time zone NOT NULL);
    CREATE INDEX "..." ON "foo" (...);
    COMMIT;
    

    Then apply execute that same SQL on your database.

    I'm using Postgres but it will be similar for other engines.

    One way is to write the content to a file:

    $ python manage.py sqlmigrate foo 0001 > foo.sql
    $ psql dbname username < foo.sql
    BEGIN
    CREATE TABLE
    CREATE INDEX
    COMMIT
    

    Another is pipe the SQL directly:

    $ python manage.py sqlmigrate foo 0001 | psql dbname username
    

    Or copy and paste it, etc.

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