Add database field - Adjust models for Django app



Frequently used standard built-in django tool. 


python makemigrations app_name

But we see that it can crash and use better than other commands to sync database with code faster

For example:

 Ignore for now, and let me handle existing rows with NULL myself..

python migrate cyclusemail

Error relation "django_content_type" already exist


comment the dependencies

vim cyclusemail/migrations/

dependencies = [

#('cyclusemail', '0003_cyclus_paused'),


python sqldiff cyclusemail

So if you've got some infinite frenzy of mistakes then?

Don't do the table creation

python squashmigrations cyclusemail 0001_initial

That didn't help.

Remove all files in migration folder and then try

python migrate cyclusemail

What is almost always works?

python sqldiff app_name  

Then you get output somthing like this:

ALTER TABLE "cyclusemail_cyclus"

        ADD COLUMN "client" varchar(256);

CREATE INDEX "cyclusemail_cyclus_client"

        ON "cyclusemail_cyclus" ("client");

CREATE INDEX "cyclusemail_cyclus_client_like"

        ON "cyclusemail_cyclus" ("client" varchar_pattern_ops);

ALTER TABLE "cyclusemail_cyclus"

        ALTER COLUMN "unsubscribe" SET NOT NULL;

ALTER TABLE "cyclusemail_cyclus"

        ALTER COLUMN "paused" SET NOT NULL;

If you need to create table / model

python sqlmigrate app_name 


Or in old version of django 1.5


python sql app_name  


And you will get plain sql statments.




-- Create model Cyclus


CREATE TABLE "cyclusemail_cyclus" ("id" serial NOT NULL PRIMARY KEY, "email" varchar(256) NOT NULL UNIQUE, "unsubscribe" varchar(256) NOT NULL, "sent" boolean NOT NULL, "paused" boolean NOT NULL, "created" timestamp with time zone NOT NULL, "status" varchar(256) NULL, "client" varchar(256) NULL, "has_redirect" boolean NOT NULL, "job_id" integer NOT NULL, "keyword_id" integer NOT NULL);




With this command you can connect to database via default client and setting from django config.


python dbshell 


copy and paste  differences.