Cannot delete some instances of model 'Appliance' because they are referenced through protected foreign keys: 'Share.appliance'.

4 users found this article helpful

Symptoms

The following error is logged when trying to install and import an environment backup:

 ----------
                            ID: /opt/awingu/awingu-core/virtualenv/bin/python manage.py prepare_database_takeover /tmp/smc/install_fixtures.json
                      Function: cmd.run
                        Result: False
                       Comment: Command "/opt/awingu/awingu-core/virtualenv/bin/python manage.py prepare_database_takeover /tmp/smc/install_fixtures.json" run
                       Started: 09:49:01.158379
                      Duration: 2408.693
                       Changes:
                                ----------
                                pid:
                                    10781
                                retcode:
                                    1
                                stderr:
                                    Traceback (most recent call last):
                                      File "/opt/awingu/awingu-core/virtualenv/lib/python3.10/site-packages/awingucore/manage.py", line 10, in
                                        execute_from_command_line(sys.argv)
                                      File "/opt/awingu/awingu-core/virtualenv/lib/python3.10/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
                                        utility.execute()
                                      File "/opt/awingu/awingu-core/virtualenv/lib/python3.10/site-packages/django/core/management/__init__.py", line 413, in execute
                                        self.fetch_command(subcommand).run_from_argv(self.argv)
                                      File "/opt/awingu/awingu-core/virtualenv/lib/python3.10/site-packages/django/core/management/base.py", line 354, in run_from_argv
                                        self.execute(*args, **cmd_options)
                                      File "/opt/awingu/awingu-core/virtualenv/lib/python3.10/site-packages/django/core/management/base.py", line 398, in execute
                                        output = self.handle(*args, **options)
                                      File "/opt/awingu/awingu-core/virtualenv/lib/python3.10/site-packages/awingucore/update/management/commands/prepare_database_takeover.py", line 29, in handle
                                        Appliance.objects.all().delete()
                                      File "/opt/awingu/awingu-core/virtualenv/lib/python3.10/site-packages/django/db/models/query.py", line 745, in delete
                                        collector.collect(del_query)
                                      File "/opt/awingu/awingu-core/virtualenv/lib/python3.10/site-packages/django/db/models/deletion.py", line 302, in collect
                                        raise ProtectedError(
                                    django.db.models.deletion.ProtectedError: ("Cannot delete some instances of model 'Appliance' because they are referenced through protected foreign keys: 'Share.appliance'.", {})
                                stdout:
                  ----------

Cause

During the install we remove the existing appliances from the external db and replace them with the new one. However, this triggers an error when shares are still pointing to these appliance(s).

Resolution

This issue is fixed in newer releases. Upgrade to at least Awingu 5.4.4.

Was this article helpful?

Tell us how we can improve it.