Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration documents.0037_delete_documentresourcelink may fail #12591

Open
etj opened this issue Sep 19, 2024 · 0 comments · May be fixed by #12592
Open

Migration documents.0037_delete_documentresourcelink may fail #12591

etj opened this issue Sep 19, 2024 · 0 comments · May be fixed by #12592
Assignees

Comments

@etj
Copy link
Contributor

etj commented Sep 19, 2024

The migration adds a linkedResource to each document having an associated DocumentResourceLink.

It happened that the migration failed in an instance because the related Resource was not found;

here the log
   Applying documents.0037_delete_documentresourcelink...Traceback (most recent call last):
   File "/usr/local/lib/python3.10/dist-packages/django/db/backends/base/base.py", line 313, in _commit
     return self.connection.commit()
 psycopg2.errors.ForeignKeyViolation: insert or update on table "base_linkedresource" violates foreign key constraint "base_linkedresource_target_id_90785fa2_fk_base_resourcebase_id"
 DETAIL:  Key (target_id)=(181) is not present in table "base_resourcebase".
 
 
 The above exception was the direct cause of the following exception:
 
 Traceback (most recent call last):
   File "/usr/src/masdap/manage.py", line 31, in <module>
     execute_from_command_line(sys.argv)
   File "/usr/local/lib/python3.10/dist-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
     utility.execute()
   File "/usr/local/lib/python3.10/dist-packages/django/core/management/__init__.py", line 436, in execute
     self.fetch_command(subcommand).run_from_argv(self.argv)
   File "/usr/local/lib/python3.10/dist-packages/django/core/management/base.py", line 412, in run_from_argv
     self.execute(*args, **cmd_options)
   File "/usr/local/lib/python3.10/dist-packages/django/core/management/base.py", line 458, in execute
     output = self.handle(*args, **options)
   File "/usr/local/lib/python3.10/dist-packages/django/core/management/base.py", line 106, in wrapper
     res = handle_func(*args, **kwargs)
   File "/usr/local/lib/python3.10/dist-packages/django/core/management/commands/migrate.py", line 356, in handle
     post_migrate_state = executor.migrate(
   File "/usr/local/lib/python3.10/dist-packages/django/db/migrations/executor.py", line 135, in migrate
     state = self._migrate_all_forwards(
   File "/usr/local/lib/python3.10/dist-packages/django/db/migrations/executor.py", line 167, in _migrate_all_forwards
     state = self.apply_migration(
   File "/usr/local/lib/python3.10/dist-packages/django/db/migrations/executor.py", line 249, in apply_migration
     with self.connection.schema_editor(
   File "/usr/local/lib/python3.10/dist-packages/django/db/backends/base/schema.py", line 168, in __exit__
     self.atomic.__exit__(exc_type, exc_value, traceback)
   File "/usr/local/lib/python3.10/dist-packages/django/db/transaction.py", line 263, in __exit__
     connection.commit()
   File "/usr/local/lib/python3.10/dist-packages/django/utils/asyncio.py", line 26, in inner
     return func(*args, **kwargs)
   File "/usr/local/lib/python3.10/dist-packages/django/db/backends/base/base.py", line 337, in commit
     self._commit()
   File "/usr/local/lib/python3.10/dist-packages/django/db/backends/base/base.py", line 312, in _commit
     with debug_transaction(self, "COMMIT"), self.wrap_database_errors:
   File "/usr/local/lib/python3.10/dist-packages/django/db/utils.py", line 91, in __exit__
     raise dj_exc_value.with_traceback(traceback) from exc_value
   File "/usr/local/lib/python3.10/dist-packages/django/db/backends/base/base.py", line 313, in _commit
     return self.connection.commit()
 django.db.utils.IntegrityError: insert or update on table "base_linkedresource" violates foreign key constraint "base_linkedresource_target_id_90785fa2_fk_base_resourcebase_id"
 DETAIL:  Key (target_id)=(181) is not present in table "base_resourcebase".

This may probably be caused by the linked layers to have been removed, and the link was not removed accordingly (was there a signal?). The DB did not have a FK on the resource, because the linked object could be anything -- on the DB where the error happened the linked resources were layers:

DB content details
select * from documents_documentresourcelink link 
where not exists (select * from base_resourcebase br where link.object_id  = br.id )

results:

id|object_id|content_type_id|document_id|
--+---------+---------------+-----------+
26|      181|             41|        271|
34|        5|             41|        252|
35|        5|             41|        256|

The 3 resources have type 41:

select * from django_content_type where id = 41
id|app_label|model  |
--+---------+-------+
41|layers   |dataset|

The migration should exclude links that have no existing target.

Found on GeoNode 4.3.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant