Cannot run migrations on postgres with a custom user model on a clean database
Issue #58
resolved
Hi
It seems that django is creating all of postman's tables before migrations are run for the remaining apps which contain custom User model that postman depends on.
This seems to be due to postman not using migrations.
Creating migrations for postman using 'python manage.py makemigrations postman' solves the problem.
Migrate output with sql output enabled in logging settings:
# python manage.py migrate -v3
[24/Jun/2015 06:49:21] DEBUG [django.db.backends:89] (0.003)
SELECT c.relname, c.relkind
FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('r', 'v')
AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
AND pg_catalog.pg_table_is_visible(c.oid); args=None
[24/Jun/2015 06:49:21] DEBUG [django.db.backends:89] (0.001) SELECT "django_migrations"."app", "django_migrations"."name" FROM "django_migrations"; args=()
Operations to perform:
Synchronize unmigrated apps: staticfiles, messages, postman
Apply all migrations: admin, contenttypes, testapp, auth, sessions
Synchronizing apps without migrations:
[24/Jun/2015 06:49:21] DEBUG [django.db.backends:89] (0.001)
SELECT c.relname, c.relkind
FROM pg_catalog.pg_class c
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('r', 'v')
AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
AND pg_catalog.pg_table_is_visible(c.oid); args=None
Running pre-migrate handlers for application admin
Running pre-migrate handlers for application auth
Running pre-migrate handlers for application contenttypes
Running pre-migrate handlers for application sessions
Running pre-migrate handlers for application postman
Running pre-migrate handlers for application testapp
Creating tables...
Processing postman.Message model
[24/Jun/2015 06:49:21] DEBUG [django.db.backends:89] (0.001) SAVEPOINT "s139702074087232_x1"; args=None
Creating table postman_message
[24/Jun/2015 06:49:21] DEBUG [django.db.backends.schema:102] CREATE TABLE "postman_message" ("id" serial NOT NULL PRIMARY KEY, "subject" varchar(120) NOT NULL, "body" text NOT NULL, "sender_id" integer NULL, "recipient_id" integer NULL, "email" varchar(254) NOT NULL, "parent_id" integer NULL, "thread_id" integer NULL, "sent_at" timestamp with time zone NOT NULL, "read_at" timestamp with time zone NULL, "replied_at" timestamp with time zone NULL, "sender_archived" boolean NOT NULL, "recipient_archived" boolean NOT NULL, "sender_deleted_at" timestamp with time zone NULL, "recipient_deleted_at" timestamp with time zone NULL, "moderation_status" varchar(1) NOT NULL, "moderation_by_id" integer NULL, "moderation_date" timestamp with time zone NULL, "moderation_reason" varchar(120) NOT NULL); (params None)
[24/Jun/2015 06:49:21] DEBUG [django.db.backends:89] (0.022) CREATE TABLE "postman_message" ("id" serial NOT NULL PRIMARY KEY, "subject" varchar(120) NOT NULL, "body" text NOT NULL, "sender_id" integer NULL, "recipient_id" integer NULL, "email" varchar(254) NOT NULL, "parent_id" integer NULL, "thread_id" integer NULL, "sent_at" timestamp with time zone NOT NULL, "read_at" timestamp with time zone NULL, "replied_at" timestamp with time zone NULL, "sender_archived" boolean NOT NULL, "recipient_archived" boolean NOT NULL, "sender_deleted_at" timestamp with time zone NULL, "recipient_deleted_at" timestamp with time zone NULL, "moderation_status" varchar(1) NOT NULL, "moderation_by_id" integer NULL, "moderation_date" timestamp with time zone NULL, "moderation_reason" varchar(120) NOT NULL); args=None
[24/Jun/2015 06:49:21] DEBUG [django.db.backends:89] (0.000) RELEASE SAVEPOINT "s139702074087232_x1"; args=None
Running deferred SQL...
[24/Jun/2015 06:49:21] DEBUG [django.db.backends:89] (0.007) ALTER TABLE "postman_message" ADD CONSTRAINT "postman_message_sender_id_3031ea7b2c020912_fk_testapp_myuser_id" FOREIGN KEY ("sender_id") REFERENCES "testapp_myuser" ("id") DEFERRABLE INITIALLY DEFERRED; args=None
Traceback (most recent call last):
File "manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/gert/.virtualenvs/test/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/home/gert/.virtualenvs/test/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 330, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/gert/.virtualenvs/test/local/lib/python2.7/site-packages/django/core/management/base.py", line 390, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/gert/.virtualenvs/test/local/lib/python2.7/site-packages/django/core/management/base.py", line 441, in execute
output = self.handle(*args, **options)
File "/home/gert/.virtualenvs/test/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 179, in handle
created_models = self.sync_apps(connection, executor.loader.unmigrated_apps)
File "/home/gert/.virtualenvs/test/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 317, in sync_apps
cursor.execute(statement)
File "/home/gert/.virtualenvs/test/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/gert/.virtualenvs/test/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/home/gert/.virtualenvs/test/local/lib/python2.7/site-packages/django/db/utils.py", line 97, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/gert/.virtualenvs/test/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 62, in execute
return self.cursor.execute(sql)
django.db.utils.ProgrammingError: relation "testapp_myuser" does not exist
Comments (2)
-
reporter -
repo owner - changed status to resolved
Fixed with the addition of migration files. Thanks Gert.
- Log in to comment
Hi, would it help if I created a PR for this?