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.

 


 class Meta: unique_together




If you try to add a field with unique_together .

unique_together = (("email", "client"),)


Then no field has to be changed, but an index and that is a problem. What I see is that this constraint is not applied.

python manage.py sqldiff job


-- No differences

Important: Don't forget that to do apache reload or restart. Because old code will still do validations even if database is modified..

But you see that index is not working as it should.

Then you can switch to SQL depending on your database. Postgres for example

SELECT tablename, indexname, indexdef FROM pg_indexes;


Or filtered result

SELECT  * from pg_indexes where tablename like '%spontaneousmail_spontaneousmail%';

 

Then find the name a of index that you are not using and take place. You can remove them.


ALTER TABLE name DROP CONSTRAINT constran_name;


DROP INDEX index_name;







Comments