Unsafe service model import creates bad migrations
Due to some logic in the Django migration engine, when an app imports a Service model such as DiscordUser into its own models in an unsafe way (loading the model without the app enabled) if makemigrations is run for any reason (such as another app) a bad migration will be created for the permission objects, breaking future migrations for the service.
The error will appear as shown, possibly even months down the line if an install enables discord at a later date
Applying discord.0002_service_permissions
...
File "/home/allianceserver/venv/auth/lib/python3.6/site-packages/django/db/models/query.py", line 436, in get num if not limit or num < limit else 'more than %s' % (limit - 1), **fake**.MultipleObjectsReturned: get() returned more than one Permission -- it returned 2!
The bad migration needs to be identified, unmigrated and deleted, some sample instructions are provided but paths may very with OS/installs and the name of the particular migration may vary credit @aaronkable
The steps I needed to do to fix
## Set the working directory
cd /home/allianceserver/myauth
## check for migrations
ls -la ../venv/myauth/lib/python3.8/site-packages/allianceauth/services/migrations
## There will be an oddball here, migrate it away back to most recent services migration 0003 as of now
## https://gitlab.com/allianceauth/allianceauth/-/tree/master/allianceauth/services/modules/discord/migrations
python manage.py migrate services
0003_remove_broken_link
## remove the bad migration
rm ../venv/auth/lib/python3.8/site-packages/allianceauth/services/migrations/0004_discorduser.py
## migrate again
python manage.py migrate
Edited by Ariel Rin