Skip to content

fix: support reverse-direction (ManyToOne) nested deep insert#37

Merged
PepperPix merged 2 commits into
betafrom
fix/deep-insert-nested-reverse-direction
May 26, 2026
Merged

fix: support reverse-direction (ManyToOne) nested deep insert#37
PepperPix merged 2 commits into
betafrom
fix/deep-insert-nested-reverse-direction

Conversation

@PepperPix

Copy link
Copy Markdown
Contributor

ProcessOneToMany now handles both directions:

  • Parent is Referenced side → sets FK on children (existing behavior)
  • Parent is Referencing side → creates child first, then sets FK on parent

This fixes nested deep inserts like Calendar → CalendarRule → InnerCalendar where the inner relationship goes from the Referencing to the Referenced side.

ProcessOneToMany now handles both directions:
- Parent is Referenced side → sets FK on children (existing behavior)
- Parent is Referencing side → creates child first, then sets FK on parent

This fixes nested deep inserts like Calendar → CalendarRule → InnerCalendar
where the inner relationship goes from the Referencing to the Referenced side.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the deep-insert implementation so OneToManyRelationshipMetadata relationships can be processed from either side, enabling nested deep inserts where the “parent” entity is actually on the referencing (many) side of the relationship (e.g., Calendar → CalendarRule → InnerCalendar).

Changes:

  • Extend DeepInsertProcessor.ProcessOneToMany to support reverse-direction deep insert by creating the referenced child first, then updating the parent lookup.
  • Add a regression test covering a nested deep insert that crosses a reverse-direction (ManyToOne) relationship.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
tests/Digitall.Dataverse.Testing.Tests/OrganizationRequests/CreateFakeDeepInsertTests.cs Adds a test validating reverse-direction nested deep insert sets the FK on the referencing-side parent.
src/Digitall.Dataverse.Testing/OrganizationRequests/DeepInsertProcessor.cs Adjusts one-to-many deep insert handling to support both referenced-side and referencing-side parents.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +82 to +95
else if (metadata.ReferencingEntity == parentLogicalName)
{
// Parent is the "many" side → create child first, then set FK on parent pointing to child
foreach (var child in children.Entities)
{
var childId = state.Create(child);
var childRef = new EntityReference(metadata.ReferencedEntity, childId);

foreach (var child in children.Entities)
var parentUpdate = new Entity(parentLogicalName, parentId)
{
[metadata.ReferencingAttribute] = childRef
};
state.Update(parentUpdate);
}
@github-actions

github-actions Bot commented May 26, 2026

Copy link
Copy Markdown

Qodana Community for .NET

40 new problems were found

Inspection name Severity Problems
Redundant nullable warning suppression expression 🔶 Warning 3
Nullability of type argument doesn't match constraint type. 🔶 Warning 2
Use collection expression syntax ◽️ Notice 19
Inconsistent Naming ◽️ Notice 6
Convert delegate variable into local function ◽️ Notice 5
Use object or collection initializer when possible ◽️ Notice 5
View the detailed Qodana report

To be able to view the detailed Qodana report, you can either:

To get *.log files or any other Qodana artifacts, run the action with upload-result option set to true,
so that the action will upload the files as the job artifacts:

      - name: 'Qodana Scan'
        uses: JetBrains/qodana-action@v2026.1.0
        with:
          upload-result: true
Contact Qodana team

Contact us at qodana-support@jetbrains.com

A lookup attribute can only reference one record. The N:1 (reverse)
branch now throws an InvalidArgument fault when multiple children are
provided, preventing silent data loss where only the last child would
be linked. Also validates that the child's logical name matches
metadata.ReferencedEntity.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@PepperPix
PepperPix merged commit 4242368 into beta May 26, 2026
5 of 6 checks passed
@PepperPix
PepperPix deleted the fix/deep-insert-nested-reverse-direction branch May 26, 2026 08:32
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 1.1.0-beta.8 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants