0

I'm deploying my Django project to a VPS using Dokku. My project uses a CustomUser model, and there are two apps in the project: accounts which has the CustomUser and gradebook.

During deployment the process runs makemigrations and migrate. After deployment when I run python manage.py showmigrations I get the following:

account
 [X] 0001_initial
 [X] 0002_email_max_length
accounts
 (no migrations)
admin
 [X] 0001_initial
 [X] 0002_logentry_remove_auto_add
 [X] 0003_logentry_add_action_flag_choices
auth
 [X] 0001_initial
 [X] 0002_alter_permission_name_max_length
 [X] 0003_alter_user_email_max_length
 [X] 0004_alter_user_username_opts
 [X] 0005_alter_user_last_login_null
 [X] 0006_require_contenttypes_0002
 [X] 0007_alter_validators_add_error_messages
 [X] 0008_alter_user_username_max_length
 [X] 0009_alter_user_last_name_max_length
 [X] 0010_alter_group_name_max_length
 [X] 0011_update_proxy_permissions
 [X] 0012_alter_user_first_name_max_length
contenttypes
 [X] 0001_initial
 [X] 0002_remove_content_type_name
gradebook
 (no migrations)
sessions
 [X] 0001_initial
sites
 [X] 0001_initial
 [X] 0002_alter_domain_unique

Where account is from django-allauth. It looks like my apps are not being migrated. So I then do:

me@myserver:/home$ dokku run gradebook python manage.py makemigrations accounts
success
Migrations for 'accounts':
  accounts/migrations/0001_initial.py
    - Create model CustomUser
me@myserver:/home$

And it looks like things are ready migrate. I then run migrate and get an error django - ValueError: Dependency on app with no migrations: accounts:

shmish@Henry:/home$ dokku run gradebook python manage.py migrate accounts
success
Traceback (most recent call last):
  File "/app/.heroku/python/lib/python3.8/site-packages/django/db/migrations/loader.py", line 174, in check_key
    return self.graph.root_nodes(key[0])[0]
IndexError: list index out of range

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/base.py", line 89, in wrapped
    res = handle_func(*args, **kwargs)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 92, in handle
    executor = MigrationExecutor(connection, self.migration_progress_callback)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/db/migrations/executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/db/migrations/loader.py", line 53, in __init__
    self.build_graph()
  File "/app/.heroku/python/lib/python3.8/site-packages/django/db/migrations/loader.py", line 235, in build_graph
    self.add_external_dependencies(key, migration)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/db/migrations/loader.py", line 199, in add_external_dependencies
    parent = self.check_key(parent, key[0])
  File "/app/.heroku/python/lib/python3.8/site-packages/django/db/migrations/loader.py", line 181, in check_key
    raise ValueError("Dependency on app with no migrations: %s" % key[0])
ValueError: Dependency on app with no migrations: accounts
2021/08/25 16:19:16 exit status 1

At this point I am unable to migrate either of my apps. Another thing I find strange about this is that I'm still able to createsuperuser, go into the shell, from accounts.models import CustomUser and successfully query CustomUser.

This error looks similar to this: ValueError: Dependency on app with no migrations: account but I don't see a solution.

Next I re-created my database on my dev and prod servers. On dev I re-ran the initial migrations and then committed to my repository. I then deployed to my production server. The migrations appear to work but I am unable to get past the login and signup forms. I've been unable to determine what the exact error is, but the page times out and gives a message "We're sorry, but something went wrong." which I assume is an nginx message.

After a few attempts at deploys to fix the problem I get new migration errors:

 Your models in app(s): 'account' have changes that are not yet reflected in a migration, and so won't be applied.
  Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.

This is an endless circle of makemigrations which appear to work, then migrate, then the error comes back up.

Note - I'm able to go into the shell and manually create a CustomUser object, but I'm unable to get past the forms on the first page of the website.

This is now looking just like this error from 2015: Django Heroku Error "Your models have changes that are not yet reflected in a migration"

All of this works fine on my dev environment, the big difference being that it is Docker while prod is Dokku.

Doug Smith
  • 500
  • 7
  • 21
  • The normal workflow is to create migrations before deployment and check them into your repository. On systems like heroku and dokku, the file system is ephemeral, so you can’t rely on files you save there (e.g. media files and in this case migration files) being there later. One last comment - I would avoid automatically running migrations as part of the deployment. As your project gets more complex, you might need greater control about when migrations are run. – Alasdair Aug 25 '21 at 16:44
  • If the migration files are ephemeral, how can we run migrations if not through the deployment? Do we manually upload migration files to the server somewhere and then run migrations? Ideally I had meant to have a db that didn't require any migrations after the initial deployment and checking them into the repository. – Doug Smith Aug 25 '21 at 21:37
  • 1
    The migration files aren't ephemeral, it's the file system on systems like Heroku. You would usually run `makemigrations` in your dev environment. Commit `accounts/migrations/0001_initial.py` to your repository, and then deploy. Then you should be able to run `migrate` with dokku. – Alasdair Aug 26 '21 at 09:39
  • 1
    If the `CustomUser` table has already been created, then your migrations and db are out of sync. You might be able to run `manage.py migrate accounts --fake-initial` ([docs](https://docs.djangoproject.com/en/3.2/ref/django-admin/#cmdoption-migrate-fake-initial)), but it would be safest to start with a fresh database if that's possible for you. – Alasdair Aug 26 '21 at 09:42
  • I am running my `makemigrations` in dev environment, committing, and then deploying. I have re-created the database (and app) several times. I am no longer getting the error above but I get "Your models in app(s): 'account' have changes that are not yet reflected in a migration". I will edit the question, as I believe this is all related. – Doug Smith Aug 26 '21 at 09:56
  • If `showmigrations` still shows `accounts (no migrations)`, then somehow the `accounts/migrations/0001_initial` migration isn't in your deployed environment. Since you say you've committed and deployed those files, I can't tell what the problem is from here. – Alasdair Aug 26 '21 at 10:14
  • Thanks for your time on this. I think I’ve turned this into a very poorly worded question I don’t know if i should edit the first half of the question or just let it die. My apologies. After recreating the dev and prod databases the `showmigrations` looked correct. Then it goes into the loop of `account` has changes that are not reflected in the migrations.` – Doug Smith Aug 26 '21 at 15:58
  • The question isn't poorly worded, but it's very long now, so it's hard to tell whether the problem is to do with the migrations you've created/committed as I first thought, or something specific to Dokku or allauth. My last guess, is that you might be hitting [this issue](https://github.com/pennersr/django-allauth/issues/2826) in allauth. – Alasdair Aug 26 '21 at 22:16

1 Answers1

0

check your git ignore, makesure folder migration comment or delete from gitignore file