Skip to content

Commit

Permalink
use ignore_conflicts when inserting references
Browse files Browse the repository at this point in the history
don't pass ignore_conflicts if backend is mssql

check for feature not DB engine
  • Loading branch information
chris48s authored and jacobtoppm committed Oct 13, 2023
1 parent 912e2b6 commit 4219488
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Changelog
* Fix: Prevent crash on snippet inspect view when displaying a null foreign key to an image (Sage Abdullah)
* Fix: Ensure that pages in moderation show as "Live + In Moderation" in the page explorer rather than "Live + Draft" (Sage Abdullah)
* Fix: Prevent error when updating reference index for objects with a lazy ParentalKey-related object (Chris Shaw)
* Fix: Ignore conflicts when inserting reference index entries to prevent race conditions causing uniqueness errors (Chris Shaw)
* Docs: Document `WAGTAILADMIN_BASE_URL` on "Integrating Wagtail into a Django project" page (Shreshth Srivastava)
* Docs: Replace incorrect screenshot for authors listing on tutorial (Shreshth Srivastava)
* Docs: Add documentation for building non-model-based choosers using the _queryish_ library (Matt Westcott)
Expand Down
1 change: 1 addition & 0 deletions docs/releases/5.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ depth: 1
* Prevent crash on snippet inspect view when displaying a null foreign key to an image (Sage Abdullah)
* Ensure that pages in moderation show as "Live + In Moderation" in the page explorer rather than "Live + Draft" (Sage Abdullah)
* Prevent error when updating reference index for objects with a lazy ParentalKey-related object (Chris Shaw)
* Ignore conflicts when inserting reference index entries to prevent race conditions causing uniqueness errors (Chris Shaw)

### Documentation

Expand Down
10 changes: 8 additions & 2 deletions wagtail/models/reference_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from django.contrib.contenttypes.fields import GenericForeignKey, GenericRel
from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.db import connection, models
from django.utils.functional import cached_property
from django.utils.text import capfirst
from django.utils.translation import gettext_lazy as _
Expand Down Expand Up @@ -456,6 +456,11 @@ def create_or_update_for_object(cls, object):
# Construct the set of reference records that have been found on the object but are not
# already present in the database
new_references = references - set(existing_references.keys())

bulk_create_kwargs = {}
if connection.features.supports_ignore_conflicts:
bulk_create_kwargs["ignore_conflicts"] = True

# Create database records for those reference records
cls.objects.bulk_create(
[
Expand All @@ -470,7 +475,8 @@ def create_or_update_for_object(cls, object):
content_path_hash=cls._get_content_path_hash(content_path),
)
for to_content_type_id, to_object_id, model_path, content_path in new_references
]
],
**bulk_create_kwargs,
)

# Delete removed references
Expand Down

0 comments on commit 4219488

Please sign in to comment.