Skip to content

[4.x] Fix duplicate entry error when updating TenantPivot columns#1469

Merged
stancl merged 3 commits into
masterfrom
tenantpivot-fix
Jul 8, 2026
Merged

[4.x] Fix duplicate entry error when updating TenantPivot columns#1469
stancl merged 3 commits into
masterfrom
tenantpivot-fix

Conversation

@lukinovec

@lukinovec lukinovec commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

TriggerSyncingEvents registered the pivot attach listener on saved, which fires on both inserts and updates. So updating pivot columns on a TenantPivot (e.g. via updateExistingPivot()) re-ran the attach flow and tried to create the tenant resource again, causing a duplicate entry error.

Switched the listener to created so we only attach when the pivot record is first created (detaching already uses deleting, so using created makes things a bit more consistent).

Added a regression test before the fix (5e3eb43). The fix (d2fb4bc) then made the test pass.

Also improved the saving listener's comment a bit so that it's clear where the "central resource not available" exception actually comes from, since that wasn't obvious (also added the @throws annotation to getCentralResourceAndTenant()).

Closes #1467

Summary by CodeRabbit

  • Bug Fixes
    • Prevented duplicate synced tenant resources when updating values on an existing pivot record.
    • Improved clarity around behavior when a central resource can’t be resolved, reducing the risk of creating invalid pivot records.
  • Tests
    • Added a regression test ensuring updating pivot-only fields (without changing attachments) does not recreate or duplicate the synced tenant resource.

@coderabbitai

coderabbitai Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 5bded470-67d5-4ddc-863f-8d63c3812315

📥 Commits

Reviewing files that changed from the base of the PR and between 3156c87 and 7900457.

📒 Files selected for processing (2)
  • src/ResourceSyncing/TriggerSyncingEvents.php
  • tests/ResourceSyncingTest.php

📝 Walkthrough

Walkthrough

This PR adds docblock clarifications in TriggerSyncingEvents.php about central resource resolution failure, and adds a regression test ensuring pivot column updates do not re-create a synced tenant resource.

Changes

Resource syncing docs and regression test

Layer / File(s) Summary
Docblock clarifications
src/ResourceSyncing/TriggerSyncingEvents.php
Comments in bootTriggerSyncingEvents and an @throws docblock on getCentralResourceAndTenant describe the CentralResourceNotAvailableInPivotException path without changing behavior.
Pivot update regression test
tests/ResourceSyncingTest.php
New test adds a pivot column, creates one synced tenant resource, updates the pivot value, and asserts no duplicate tenant resource is created.

Estimated code review effort: 1 (Trivial) | ~4 minutes

Poem

A pivot twitched, but stayed the same,
No duplicate bunnies joined the game. 🐰
Docs now say what paths may roam,
And tests hop in to keep it home.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning The PR adds a regression test, but the summarized code changes only update docs and do not show the saved-to-created sync fix required by #1467. Change the pivot attach hook from saved to created, or equivalent, so syncing runs only on initial insert, then rerun the regression test.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly states the bug fix around duplicate entries when updating TenantPivot columns.
Out of Scope Changes check ✅ Passed All changes are directly related to the TenantPivot duplicate-entry bug fix and its regression coverage.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch tenantpivot-fix

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Use created() instead of saved() in TriggerSyncingEvents so that updating
pivot columns no longer attempts to attach. Attaching re-created the tenant
resource and caused a duplicate entry error (detaching already uses deleting(),
so using created() is more consistent with that anyway)
@codecov

codecov Bot commented Jul 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.37%. Comparing base (3156c87) to head (7900457).

Additional details and impacted files
@@            Coverage Diff            @@
##             master    #1469   +/-   ##
=========================================
  Coverage     86.37%   86.37%           
  Complexity     1200     1200           
=========================================
  Files           186      186           
  Lines          3524     3524           
=========================================
  Hits           3044     3044           
  Misses          480      480           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

The comment said an exception is thrown but not how -- it happens
indirectly via findCentralResource() -> getResourceClass(). Also added a @throws annotation to getCentralResourceAndTenant().
@lukinovec lukinovec marked this pull request as ready for review July 7, 2026 11:48
@lukinovec

Copy link
Copy Markdown
Contributor Author

@coderabbitai full review

@coderabbitai

coderabbitai Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Full review finished.

@lukinovec lukinovec requested a review from stancl July 7, 2026 11:56
@stancl stancl merged commit 869ad78 into master Jul 8, 2026
14 checks passed
@stancl stancl deleted the tenantpivot-fix branch July 8, 2026 01:56
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 this pull request may close these issues.

Updating pivot values on TenantPivot causes duplicate entry errors

2 participants