Aiky30 / djangocms-4-migration

BSD 3-Clause "New" or "Revised" License
5 stars 13 forks source link

django.db.utils.IntegrityError: update or delete on table "cms_page" violates foreign key constraint #17

Open rvanlaar opened 3 months ago

rvanlaar commented 3 months ago

When running the migration: manage.py cms4_migration all goes well till I'm hit with

django.db.utils.IntegrityError: update or delete on table "cms_page" violates foreign key constraint "djangocms_link_link_internal_link_id_41549c8e_fk_cms_page_id" on table "djangocms_link_link"
DETAIL:  Key (id)=(130) is still referenced from table "djangocms_link_link".

How can we resolve this?

Important versions: Python 3.9.19 psycopg 3.1.19 django 4.2.13 djangocms-4-migration 0.0.2 51a3a7b
djangocms-admin-style 3.3.1
djangocms-alias 2.0.1
djangocms-attributes-field 3.0.0
djangocms-file 3.0.1
djangocms-link 3.1.1
djangocms-picture 4.1.1
djangocms-text-ckeditor 5.1.5
djangocms-versioning 2.0.2

The log, including traceback:

Operations to perform:
  Apply all migrations: admin, api, auth, authtoken, axes, cms, contenttypes, djangocms_4_migration, djangocms_alias, djangocms_file, djangocms_link, djangocms_picture, djangocms_text_ckeditor, djangocms_versioning, djangosaml2idp, easy_thumbnails, edubroker, edubroker_monitor, filer, menus, oauth2_provider, sessions, sites, stickymessages
Running migrations:
  Applying djangocms_4_migration.0001_initial... OK
  Applying djangocms_4_migration.0002_collect_removed_data_data_migration... OK
  Applying cms.0023_placeholder_source_field... OK
  Applying cms.0024_set_plugin_absolute_position... OK
  Applying cms.0025_remove_plugin_tree_fields... OK
  Applying cms.0026_title_placeholders... OK
  Applying cms.0027_title_placeholders_data_migration.../workspace/.venv/lib/python3.9/site-packages/cms/migrations/0027_title_placeholders_data_migration.py:101: UserWarning: There's placeholders in your database with plugins in a language that's not configured These placeholders and its plugins are not in use and can be removed.
  warnings.warn(
 OK
  Applying cms.0028_remove_page_placeholders... OK
  Applying cms.0029_create_title_fields_and_url_model... OK
  Applying cms.0030_auto_20180810_0629... OK
  Applying cms.0031_remove_fields... OK
  Applying cms.0032_remove_title_to_pagecontent... OK
  Applying cms.0033_placeholder_source_data_migration... OK
  Applying cms.0034_remove_pagecontent_placeholders... OK
  Applying cms.0035_auto_20230822_2208... OK
  Applying djangocms_versioning.0001_initial... OK
  Applying djangocms_versioning.0002_delete_campaign... OK
  Applying djangocms_versioning.0003_version... OK
  Applying djangocms_versioning.0004_auto_20180730_1135... OK
  Applying djangocms_versioning.0005_remove_version_label... OK
  Applying djangocms_versioning.0006_auto_20180809_1714... OK
  Applying djangocms_versioning.0007_auto_20180813_1407... OK
  Applying djangocms_versioning.0008_auto_20180820_1754... OK
  Applying djangocms_versioning.0009_cms_pagecontent_remove_unique_constraint... OK
  Applying djangocms_versioning.0010_version_proxies... OK
  Applying djangocms_versioning.0011_version_number... OK
  Applying djangocms_versioning.0012_create_version_numbers... OK
  Applying djangocms_versioning.0013_auto_20181005_1404... OK
  Applying djangocms_versioning.0014_version_source... OK
  Applying djangocms_versioning.0015_version_modified... OK
  Applying djangocms_4_migration.0003_page_version_integration_data_migration...WARNING:djangocms_4_migration.migrations.0003_page_version_integration_data_migration: User Petra Baumann not found, falling back. (2024-06-05 13:36:31,018; 0003_page_version_integration_data_migration.py:72)
WARNING:djangocms_4_migration.migrations.0003_page_version_integration_data_migration: User XXXXXXX not found, falling back. (2024-06-05 13:36:31,021; 0003_page_version_integration_data_migration.py:72)
<SNIP>
WARNING:djangocms_4_migration.migrations.0003_page_version_integration_data_migration: User XXXX not found, falling back. (2024-06-05 13:36:32,170; 0003_page_version_integration_data_migration.py:72)
 OK
  Applying djangocms_alias.0001_initial... OK
  Applying djangocms_alias.0002_auto_20200723_1424... OK
  Applying djangocms_alias.0003_auto_20230725_1547... OK
  Applying djangocms_alias.0004_alter_aliascontent_language... OK
  Applying djangocms_text_ckeditor.0005_alter_text_cmsplugin_ptr... OK
  Applying djangocms_versioning.0016_auto_20230505_0934... OK
  Applying djangocms_versioning.0016_alter_version_content_type... OK
  Applying djangocms_versioning.0017_merge_20230514_1027... OK
Traceback (most recent call last):
  File "/workspace/.venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
  File "/workspace/.venv/lib/python3.9/site-packages/psycopg/cursor.py", line 732, in execute
    raise ex.with_traceback(None)
psycopg.errors.ForeignKeyViolation: update or delete on table "cms_page" violates foreign key constraint "djangocms_link_link_internal_link_id_41549c8e_fk_cms_page_id" on table "djangocms_link_link"
DETAIL:  Key (id)=(130) is still referenced from table "djangocms_link_link".

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/workspace/./manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/workspace/.venv/lib/python3.9/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/workspace/.venv/lib/python3.9/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/workspace/.venv/lib/python3.9/site-packages/django/core/management/base.py", line 412, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/workspace/.venv/lib/python3.9/site-packages/django/core/management/base.py", line 458, in execute
    output = self.handle(*args, **options)
  File "/workspace/.venv/lib/python3.9/site-packages/djangocms_4_migration/management/commands/cms4_migration.py", line 23, in handle
    call_command('migration_cleanup')
  File "/workspace/.venv/lib/python3.9/site-packages/django/core/management/__init__.py", line 194, in call_command
    return command.execute(*args, **defaults)
  File "/workspace/.venv/lib/python3.9/site-packages/django/core/management/base.py", line 458, in execute
    output = self.handle(*args, **options)
  File "/workspace/.venv/lib/python3.9/site-packages/djangocms_4_migration/management/commands/migration_cleanup.py", line 79, in handle
    _delete_page(page)
  File "/workspace/.venv/lib/python3.9/site-packages/djangocms_4_migration/management/commands/migration_cleanup.py", line 25, in _delete_page
    cursor.execute("DELETE FROM cms_page WHERE id = %s", [page.id])
  File "/workspace/.venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 102, in execute
    return super().execute(sql, params)
  File "/workspace/.venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(
  File "/workspace/.venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 80, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/workspace/.venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
  File "/workspace/.venv/lib/python3.9/site-packages/django/db/utils.py", line 91, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/workspace/.venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
  File "/workspace/.venv/lib/python3.9/site-packages/psycopg/cursor.py", line 732, in execute
    raise ex.with_traceback(None)
django.db.utils.IntegrityError: update or delete on table "cms_page" violates foreign key constraint "djangocms_link_link_internal_link_id_41549c8e_fk_cms_page_id" on table "djangocms_link_link"
DETAIL:  Key (id)=(130) is still referenced from table "djangocms_link_link".
fsbraun commented 2 months ago

It seems your page with ID 130 does not have any language content assigned to it. Can you check this for your v3 database? IntegrityError should also be captured in djangocms_4_migration/management/commands/migration_cleanup.py?

fsbraun commented 2 months ago

In v4 it's fine to have a Page object without assigned PageContent objects (formerly known as PageTitle)

fsbraun commented 2 months ago

@rvanlaar I just saw #12 does fix the error.

rvanlaar commented 2 months ago

Much appreciated. I'll try that PR.

fsbraun commented 2 months ago

Please let us know if it helped!

Aiky30 commented 1 month ago

@rvanlaar Did that PR fix your issue, we need others to have tried and tested changes due to the lack of test coverage by the package.

rvanlaar commented 1 month ago

I haven't had the opportunity to check it. I'll report back when I have. Might be next sprint.

Aiky30 commented 1 month ago

@rvanlaar Have you had chance to test out Fabians recommendation yet?

https://github.com/Aiky30/djangocms-4-migration/issues/17#issuecomment-2214594755

Aiky30 commented 1 month ago

I'll try and find some time to add some test coverage to this package to give us some confidence when making any changes.

rvanlaar commented 1 month ago

@Aiky30 I installed the PR #12 , and the issue is resolved.