SwiftData upsert duplicates problem or ?

Context: Importing JSON to have some data during development of SwiftData MacOS app.

One object is repeated several times in the JSON and each copy is in the decoded data and does get inserted into the context and can be saved to the container, but then there are multiple copies of the same object with the same identifier in the database.

When I qualify If the troublesome object's class with an @Attribute(.unique) identifier then the app crashes on .save. I was expecting a silent upsert.

In this case there are three @Model classes. The model has a primary object with children objects and each child may have a third object type as a property and that third object may have a distinct third object listed as one of its own properties. It is this third object which appears several times in the file since it can be associated with many children.

The insert code only specifies the primary object as I was expecting nested insertion of the children and the third object. Does this need to be handled differently?

I'd like to only insert the first instance of the third object and keep the relationship intact. Should upsert work on .insert and might there be a technical problem?

@pikes did you end up solving this? Seems wasteful to have to check for a unique object before inserting.

SwiftData upsert duplicates problem or ?
 
 
Q