I've not found a huge amount of guidance out there on this issue - and when I do find guidance there's not much to explain away some of the problems I'm having. I'm self-learning Python and Django, which is going fine, but deploying to a server is...hell. It would be great if there was someone out there with a bullet proof workflow for backing up a Django DB, and restoring to a server. With reference to virtual environments, migrations, --fake and -initial.
For my own sake - and for anyone reading - I'm going to go through exactly what I've done, and see if I can see where the mistakes are - because I'm at the brick wall stage.
The Problem
'It works on my machine' pretty much sums up exactly where I am at. It works fine. ON MY MACHINE.
I've managed to get to a point where the database (Postgresql) is restored to my server - Python Anywhere. And my server instance of Django is talking to the database, and some data is being served. However, there are then some migration issues which persist, and ultimately cause several fatal errors about certain tables not existing - like this:
Column podcast_show.type does not exist
LINE 1: SELECT "podcast_show"."id", "podcast_show"."type", "podcast_...
Running makemigrations / migrate gets me errors like this one
django.db.utils.IntegrityError: null value in column "id" violates not-null constraint
DETAIL: Failing row contains (null, curate, 0005_item_tags, 2019-04-29 12:38:32.000168+00).
The Environment
I'm on windows, developing locally on Atom. I have a virtual environment with Django 2.1 installed. I'm using GIT for version control (which I only just understand), and I have some pypy installed django applications (in the virtualENV), which thinking about it now, I have made direct changes to.
This was the first 'aha' moment where I thought I'd cracked it. The changes I've made to pypy packages in myLocalVirtualEnv (including models) were NOT included in my GIT repo which was pushed to the server. So when I've pip installed all the dependencies from requirements.txt on the server, I've got fresh installs of these apps, rather than their modified versions.
* Question 1) What's the proper workflow to avoid this? * Once I start modifying a pypy package, which is a dependency, should I move it out of the virtualenv, and into my standard django folder structure, and work on it as though it's my own app?
The Backup
This seems to be the easy bit. I've used pgAdmin4 on my local machine - right click, backup, choose an output, and off it goes. I'm doing that live right now - from my working local project.
Done. I now have a large DB(3GB uncompressed - is this huge?) database file 'appBU'. I now use SFTP(filezilla) to send this file across to my server, putting it in the same directory as my top App level. home/user/App/app
Now in a postgresql console, I make a new database.
CREATE DATABASE app;
Then I make a user for the database. I don't always do this, if I've dropped the database and tried to backup again, I just use pre-existing users. In that case, I run the command DROP DATABASE app; followed by CREATE DATABASE app; so I'm ready with a fresh database to restore to. Anyway, the users:
CREATE USER username WITH PASSWORD 'password';
ALTER ROLE username WITH PASSWORD client_encoding TO 'utf8';
ALTER ROLE username SET default_transaction_isolation TO 'read committed';
ALTER ROLE username SET timezone TO 'UTC';
GRANT ALL PRIVILEGES ON DATABASE app TO username;
Now I restore the database to the newly created empty database I just made - this is from inside a bash console (not psql console - that took a while to workout!)
pg_restore -h the-host-address-for-my-postgresql-database -p 11111 -U super -W -C app < appBU
This is where the problems start, because pg_restore is constantly unhappy as it whirrs away, these are the kinds of errors I'm getting:
pg_restore: [archiver (db)] Error from TOC entry 3347; 0 0 SEQUENCE SET django_comment_flags_id_seq postgres
pg_restore: [archiver (db)] could not execute query: ERROR: relation "public.django_comment_flags_id_seq" does not exist
LINE 1: SELECT pg_catalog.setval('public.django_comment_flags_id_seq...
^
Command was: SELECT pg_catalog.setval('public.django_comment_flags_id_seq', 1, false);
pg_restore: [archiver (db)] Error from TOC entry 3348; 0 0 SEQUENCE SET django_comments_id_seq postgres
pg_restore: [archiver (db)] could not execute query: ERROR: relation "public.django_comments_id_seq" does not exist
LINE 1: SELECT pg_catalog.setval('public.django_comments_id_seq', 17...
^
Command was: SELECT pg_catalog.setval('public.django_comments_id_seq', 17, true);
pg_restore: [archiver (db)] Error from TOC entry 3349; 0 0 SEQUENCE SET django_content_type_id_seq postgres
pg_restore: [archiver (db)] could not execute query: ERROR: relation "public.django_content_type_id_seq" does not exist
LINE 1: SELECT pg_catalog.setval('public.django_content_type_id_seq'...
^
Command was: SELECT pg_catalog.setval('public.django_content_type_id_seq', 22, true);
pg_restore: [archiver (db)] Error from TOC entry 3350; 0 0 SEQUENCE SET django_migrations_id_seq postgres
pg_restore: [archiver (db)] could not execute query: ERROR: relation "public.django_migrations_id_seq" does not exist
LINE 1: SELECT pg_catalog.setval('public.django_migrations_id_seq', ...
^
Command was: SELECT pg_catalog.setval('public.django_migrations_id_seq', 108, true);
pg_restore: [archiver (db)] Error from TOC entry 3351; 0 0 SEQUENCE SET django_site_id_seq postgres
pg_restore: [archiver (db)] could not execute query: ERROR: relation "public.django_site_id_seq" does not exist
LINE 1: SELECT pg_catalog.setval('public.django_site_id_seq', 1, tru...
^
pg_restore: [archiver (db)] Error from TOC entry 3352; 0 0 SEQUENCE SET podcast_category_id_seq postgres
pg_restore: [archiver (db)] could not execute query: ERROR: relation "public.podcast_category_id_seq" does not exist
LINE 1: SELECT pg_catalog.setval('public.podcast_category_id_seq', 1...
^
Command was: SELECT pg_catalog.setval('public.podcast_category_id_seq', 1098, true);
pg_restore: [archiver (db)] Error from TOC entry 3353; 0 0 SEQUENCE SET podcast_enclosure_id_seq postgres
pg_restore: [archiver (db)] could not execute query: ERROR: relation "public.podcast_enclosure_id_seq" does not exist
LINE 1: SELECT pg_catalog.setval('public.podcast_enclosure_id_seq', ...
^
Command was: SELECT pg_catalog.setval('public.podcast_enclosure_id_seq', 1, false);
pg_restore: [archiver (db)] Error from TOC entry 3354; 0 0 SEQUENCE SET podcast_episode_guests_id_seq postgres
pg_restore: [archiver (db)] could not execute query: ERROR: relation "public.podcast_episode_guests_id_seq" does not exist
LINE 1: SELECT pg_catalog.setval('public.podcast_episode_guests_id_s...
^
Command was: SELECT pg_catalog.setval('public.podcast_episode_guests_id_seq', 1, false);
pg_restore: [archiver (db)] Error from TOC entry 3355; 0 0 SEQUENCE SET podcast_episode_hosts_id_seq postgres
pg_restore: [archiver (db)] could not execute query: ERROR: relation "public.podcast_episode_hosts_id_seq" does not exist
LINE 1: SELECT pg_catalog.setval('public.podcast_episode_hosts_id_se...
^
Command was: SELECT pg_catalog.setval('public.podcast_episode_hosts_id_seq', 1, false);
pg_restore: [archiver (db)] Error from TOC entry 3356; 0 0 SEQUENCE SET podcast_episode_id_seq postgres
pg_restore: [archiver (db)] could not execute query: ERROR: relation "public.podcast_episode_id_seq" does not exist
LINE 1: SELECT pg_catalog.setval('public.podcast_episode_id_seq', 19...
^
Command was: SELECT pg_catalog.setval('public.podcast_episode_id_seq', 1995295, true);
pg_restore: [archiver (db)] Error from TOC entry 3357; 0 0 SEQUENCE SET podcast_show_categories_id_seq postgres
pg_restore: [archiver (db)] could not execute query: ERROR: relation "public.podcast_show_categories_id_seq" does not exist
LINE 1: SELECT pg_catalog.setval('public.podcast_show_categories_id_...
^
Command was: SELECT pg_catalog.setval('public.podcast_show_categories_id_seq', 3873, true);
pg_restore: [archiver (db)] Error from TOC entry 3358; 0 0 SEQUENCE SET podcast_show_hosts_id_seq postgres
pg_restore: [archiver (db)] could not execute query: ERROR: relation "public.podcast_show_hosts_id_seq" does not exist
LINE 1: SELECT pg_catalog.setval('public.podcast_show_hosts_id_seq',...
^
Command was: SELECT pg_catalog.setval('public.podcast_show_hosts_id_seq', 1, false);
pg_restore: [archiver (db)] Error from TOC entry 3359; 0 0 SEQUENCE SET podcast_show_id_seq postgres
pg_restore: [archiver (db)] could not execute query: ERROR: relation "public.podcast_show_id_seq" does not exist
LINE 1: SELECT pg_catalog.setval('public.podcast_show_id_seq', 27121...
^
Command was: SELECT pg_catalog.setval('public.podcast_show_id_seq', 27121, true);
pg_restore: [archiver (db)] Error from TOC entry 3360; 0 0 SEQUENCE SET podcast_show_itunes_categories_id_seq postgres
pg_restore: [archiver (db)] could not execute query: ERROR: relation "public.podcast_show_itunes_categories_id_seq" does not exist
LINE 1: SELECT pg_catalog.setval('public.podcast_show_itunes_categor...
^
Command was: SELECT pg_catalog.setval('public.podcast_show_itunes_categories_id_seq', 1, false);
pg_restore: [archiver (db)] Error from TOC entry 3361; 0 0 SEQUENCE SET podcast_speaker_id_seq postgres
pg_restore: [archiver (db)] could not execute query: ERROR: relation "public.podcast_speaker_id_seq" does not exist
LINE 1: SELECT pg_catalog.setval('public.podcast_speaker_id_seq', 1,...
^
Command was: SELECT pg_catalog.setval('public.podcast_speaker_id_seq', 1, false);
pg_restore: [archiver (db)] Error from TOC entry 3362; 0 0 SEQUENCE SET taggit_tag_id_seq postgres
pg_restore: [archiver (db)] could not execute query: ERROR: relation "public.taggit_tag_id_seq" does not exist
LINE 1: SELECT pg_catalog.setval('public.taggit_tag_id_seq', 49920, ...
^
Command was: SELECT pg_catalog.setval('public.taggit_tag_id_seq', 49920, true);
pg_restore: [archiver (db)] Error from TOC entry 2912; 2604 34102 DEFAULT taggit_taggeditem id postgres
pg_restore: [archiver (db)] could not execute query: ERROR: relation "public.taggit_taggeditem_id_seq" does not exist
Command was: ALTER TABLE ONLY public.taggit_taggeditem ALTER COLUMN id SET DEFAULT nextval('public.taggit_taggeditem_id_seq'::regclass);
However - if I now go to my domain, I can see the data is there and being accessed/served by Django. The errors which persist kill certain pages which are querying some of the data mentioned above. If I try and do makemigrations/migrate, i get this:
django.db.utils.IntegrityError: null value in column "id" violates not-null constraint
DETAIL: Failing row contains (null, curate, 0006_auto_20190429_1108, 2019-04-29 15:50:36.927028+00).
For the record, the 'podcast' app it's referring to is the modified app from my virtualenv. However, in this case I've moved it from the virtualenv and relocated it to the git repo inside my project - transferring all the code with it.
Does anyone know what's happening?
I should add, the codebase is updated to pythonanywhere via git. I stage the changes on Atom, then commit them. Then push them to github, then pull them down to the server with a bash command.