Skip to content

CBEF-134: Write a derived owner once when its base-declared owned type changes - #197

Open
davidkelly wants to merge 2 commits into
mainfrom
owner-write-derived-type
Open

davidkelly wants to merge 2 commits into
mainfrom
owner-write-derived-type

Conversation

@davidkelly

@davidkelly davidkelly commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

The issue in brief: with TPH inheritance, a derived entity with an owned type declared on its base type was written twice in one SaveChanges. When an owned entity changes, the save also writes the owner's document unless the owner is already written. That check looked the owner up under the type that declares the owned navigation (the base type), while the main pass had recorded it under its actual (derived) type. So the derived entity got an insert and an upsert sent at the same time, and the insert failed with DocumentExistsException whenever the upsert landed first. This made the JsonQuery spec fixture's seed fail (all 438 tests in CI).

Change

  • The owner pass writes an owner only when it is unchanged (the main pass writes every added, modified or deleted entity), once per owner entry. Before, it looked the owner up by a string of the declaring type's name and the key, which missed a derived owner. A deleted owner can no longer get an upsert next to its delete.
  • Write planning (main pass + owner pass) moves into PlanWrites, which builds the pending writes without sending them. SaveChangesAsync calls it and then sends the writes as before, so behaviour does not change. It makes the double write testable without a cluster.
  • CHANGELOG (Fixed).

Tests

  • Unit (CouchbaseOwnerWriteTests, no cluster): plans the writes and checks that each document is written once. Cases: an added derived entity, base and derived added together, a modified derived entity with a changed base-declared owned type, only the owned type changed (one upsert), a deleted derived entity (only a delete), one owner with two changed owned types (written once), and two owner types with the same class name and key (both written). Without the fix, the first three fail; the others guard the owner pass. 1,238/1,238 pass.
  • Integration (DerivedOwnerWriteTests): adds a base and a derived entity, then changes the derived entity and its base-declared owned type, and checks the stored documents with the SDK. Without the fix it failed 3 of 3 runs with DocumentExistsException. Full suite 410/410.
  • JsonQuery seed on a local cluster (traced earlier): 16 writes instead of 17, and 0 of 10 runs failed (2 of 8 failed before).

The integration test reads the documents directly, because two other bugs found while writing it would get in the way. Owned references with a property of the same name are flattened into one field. An OwnsOne declared on a TPH base type reads back with null properties. Both are part of CBEF-136. The test gives Collar its own property name for that reason.

JsonQuery tests still fail after this. They also need CBEF-135 (#196) and the known "relational model built twice for JSON entity types" fix.

Ticket: CBEF-134

🤖 Generated with Claude Code

…e changes

Motivation
==========
When an owned entity changes, the save also writes its owner's
document unless the owner is already written. For an owned type
declared on a base type, that check used the base type, but the main
pass recorded the owner under its derived type. So a derived owner
was written twice in one save: an insert and an upsert sent
concurrently, and the insert failed with DocumentExistsException
whenever the upsert landed first.

Modification
============
- Use the owner entry's actual entity type in the owner pass.
- Move write planning into PlanWrites, which builds the pending
  writes without sending them, so tests can check them without a
  cluster.

Results
=======
4 unit tests count the planned writes (3 fail without the fix) and an
integration test checks the stored documents (it failed 3 of 3 runs
without the fix). 1,235 unit and 410 integration tests pass.

@emilienbev emilienbev left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Good fix, and PlanWrites is a nice seam. The tests drive a real change tracker with no cluster.

My concerns.

  • The lookup is still a string of the CLR short type name plus the key, and the keyspace is not part of it. So the same class of bug can come back. A set of owner entries would remove it.
  • The worst case of the old bug has no test. A deleted derived owner used to plan an upsert next to the delete, so the document could come back.
  • Bigger picture, see my note on #192 about the owned type path.

FYI, not for this PR. EF lets a deleted entity and a new one share a key in one save. We then send a delete and an insert for the same document in parallel. Cosmos handles that through SharedIdentityEntry. Maybe a ticket?

Motivation
==========
The owner pass skipped an owner whose "ClrType.Name:key" string was
already written. Two types with the same short name and key looked
like one owner, so the second owner was not written.

Modification
============
- Write an owner in the owner pass only if it is Unchanged; the main
  pass writes every added, modified or deleted owner. Dedupe by entry.
- Remove writtenRoots. Walk to the root owner in FindRootOwner, with
  StateManager.FindPrincipal per hop.
- Add unit tests: deleted derived owner, two changed owned types on one
  owner, and same-named owner types with the same key.
- Drop the integration test's collection in a finally; refer to
  CBEF-136 for the owned-type field mapping.

Results
=======
1,238 unit tests pass. The same-name test fails on the previous code;
the new tests fail on the declaring-type logic.
@davidkelly

Copy link
Copy Markdown
Contributor Author

Thanks @emilienbev. All seven points are in e234cfd. With the state check, the deleted-owner case can no longer plan an upsert, and there is now a test for it. The owned-type design point is in CBEF-136. For the delete + insert with a shared key, I filed CBEF-138.

@emilienbev emilienbev left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Looks good, a few small comments on the changelog and test comments.

public Collar Collar { get; set; } = null!;
}

// Its own type and property name, and the test reads the documents directly, so it does not

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Small thing. This comment says the test does not depend on how owned types map to fields. But lines 49 to 51 assert the flat Text and Color fields, and CBEF-136 plans to prefix them (Tag_Text). Could we say that here instead, so whoever lands CBEF-136 knows these asserts will change?

Comment thread CHANGELOG.md

### Fixed

- **A derived entity is written once when an owned type declared on its base type changes.** With

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Small thing. On main this bug also hit deletes. Removing a derived owner planned an upsert next to the delete, so the document could survive the delete. Could we add a sentence for that?

namespace Couchbase.EntityFrameworkCode.IntegrationTests.Tests;

/// <summary>
/// A derived entity (TPH) with an owned type declared on its base type used to be written twice in

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nit: this summary still tells the history of the bug, like the unit test one did. Maybe keep only the last part, that it checks the round trip on a cluster and reads the documents directly?

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.

2 participants