Add database field - Adjust models for Django app

 


 

Frequently used standard built-in django tool. 

 

python manage.py 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 manage.py migrate cyclusemail


Error relation "django_content_type" already exist

 

comment the dependencies

vim cyclusemail/migrations/0004_auto_20200803_1142.py



dependencies = [

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

]


python manage.py sqldiff cyclusemail



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

Don't do the table creation



python manage.py squashmigrations cyclusemail 0001_initial

That didn't help.


Remove all files in migration folder and then try


python manage.py migrate cyclusemail



What is almost always works?

python manage.py 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 manage.py sqlmigrate app_name 

 

Or in old version of django 1.5

 

python manage.py sql app_name  

 

And you will get plain sql statments.

0001_initial


BEGIN;

--

-- 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 manage.py dbshell 

 

copy and paste  differences.

 

Comments