In SwiftData, a Task has both an assignee and a reviewer of type User; why do the user's task lists stay empty, and how do you declare the inverses?
answer
- two links between the same pair
- inference needs exactly one candidate
- one-way relationships by default
- inverse once per pairing
basics
~20 sWith two relationships between the same two models, SwiftData cannot tell which property pairs with which, so it infers no inverse and every property stays one-way. Declare each pairing with @Relationship(inverse:) on exactly one side.
solid answer
~40 sSwiftData infers an inverse only when it can do so reliably, as for a single one-to-one or one-to-many pair. `ProjectTask.assignee` and `ProjectTask.reviewer` both point at `User`, and `User` has two arrays, `assignedTasks` and `tasksToReview`, so there are several possible pairings. SwiftData picks none, and the schema holds four one-way relationships. Setting `task.assignee = user` succeeds and saves, but `user.assignedTasks` stays empty. Declare the pairings on the `User` side, `@Relationship(inverse: \ProjectTask.assignee) var assignedTasks` and `@Relationship(inverse: \ProjectTask.reviewer) var tasksToReview`, and leave the task's properties plain. Declaring an inverse on both ends fails to compile with "circular reference resolving attached macro 'Relationship'". Many-to-many arrays need an explicit inverse for the same reason.
code
swift · 21 linesimport SwiftData
@Model
final class User {
var name: String
@Relationship(inverse: \ProjectTask.assignee)
var assignedTasks: [ProjectTask] = []
@Relationship(inverse: \ProjectTask.reviewer)
var tasksToReview: [ProjectTask] = []
init(name: String) { self.name = name }
}
@Model
final class ProjectTask {
var title: String
var assignee: User? // no @Relationship here: each inverse is declared once
var reviewer: User?
init(title: String) { self.title = title }
}go deeper
Know that two properties describing one link are inverses, and that SwiftData only keeps both ends in sync when it knows the pairing.
Explain when SwiftData infers the inverse and when it cannot, and why declaring it on both ends fails to compile.
Diagnose the empty-list symptom from an ambiguous pair, fix the declarations, and plan the data repair and test that go with it.
Make explicit inverses a review rule for every pairing, so adding a second relationship later never silently breaks an inferred one.
## How SwiftData finds an inverse Two relationship properties are **inverses** when they describe one link from opposite ends: `ProjectTask.project` and `Project.tasks`. SwiftData needs to know the pairing to keep both ends consistent, and it learns it in one of two ways: - **Declared**, with `@Relationship(inverse:)` and a key path to the other property. - **Inferred.** Apple's documentation says SwiftData sets the inverse automatically if it can reliably infer it from the schema. "Reliably" is the key word. Inference works for a single pair with one candidate on each side, as in one-to-one (`User.profile` and `Profile.user`) or one-to-many (`Project.tasks` and `ProjectTask.project`). It does not work in two common cases: 1. **Several relationships between the same two models.** A task has an `assignee` and a `reviewer`, both of type `User`, and the user has `assignedTasks` and `tasksToReview`. Any array could pair with any to-one property. 2. **Many-to-many.** Two arrays pointing at each other are never paired automatically. ## What a missing inverse looks like Nothing fails at compile time, and the container opens. The schema simply holds independent **one-way relationships**: `ProjectTask.assignee` with no inverse, `User.assignedTasks` with no inverse, and so on. Then: - `task.assignee = alice` saves, but `alice.assignedTasks` stays empty. A "My tasks" screen that reads the array shows nothing. - Appending to `alice.assignedTasks` does not set `task.assignee`, so the two sides drift apart and can be saved disagreeing. This is the bug behind "the data is saved but the other screen never shows it". ## Declaring the pairings Each pairing gets exactly one `@Relationship(inverse:)`, on either side: ```swift @Relationship(inverse: \ProjectTask.assignee) var assignedTasks: [ProjectTask] = [] @Relationship(inverse: \ProjectTask.reviewer) var tasksToReview: [ProjectTask] = [] ``` The task's `assignee` and `reviewer` stay plain `User?` properties. With that in place, setting `task.reviewer = nil` removes the task from `tasksToReview` and leaves `assignedTasks` alone. | Situation | Inverse inferred? | What to write | |---|---|---| | One-to-one, one pair | yes | optional; declare once for clarity | | One-to-many, one pair | yes | optional; declare once for clarity | | Two or more links between the same models | no | one `inverse:` per pairing | | Many-to-many | no | one `inverse:` on either array | | A link you only follow one way | not applicable | nothing; a one-way relationship is valid | ## Declaring it twice It is tempting to put `inverse:` on both ends "for symmetry". The two macros then each need the other's declaration resolved first, and the compiler stops with `circular reference resolving attached macro 'Relationship'`. The same error appears whether the two models live in one file or in two. One side is enough, and it does not matter which. ## One-way relationships are allowed Not every relationship needs an inverse. A `Note` with `var author: User?` and no matching array on `User` is a valid one-way relationship, fine when the app never navigates from a user to their notes. The defect is the *accidental* one-way relationship: two properties written as a pair that SwiftData never paired. ## Checking what SwiftData inferred The schema the container built tells you which pairings exist, so you do not have to guess from the source: ```swift let assignee = container.schema .entitiesByName["ProjectTask"]? .relationshipsByName["assignee"] print(assignee?.inverseName ?? "no inverse") ``` For the ambiguous models this prints `no inverse`; after the fix it prints `assignedTasks`. The same check works for a many-to-many pair, and it is a cheap assertion to keep in a unit test. ## Making it hard to get wrong - In review, treat every relationship without an explicit `inverse:` as a question: is it one-way on purpose, or inferred by luck? - Name the arrays after their role (`assignedTasks`, `tasksToReview`) rather than `tasks` and `tasks2`, so each key path in `inverse:` reads as the pairing it is. - Add a unit test per pairing: set the to-one side and assert the array on the other model contains the task. - When a second relationship to an existing model is added, re-check the first: a pair that was inferred before is now ambiguous, so give it an explicit inverse in the same change. - If the store is synced with CloudKit, Apple's guidance is that when SwiftData cannot infer an inverse you explicitly set it before saving, because CloudKit processes changes in an indeterminate order.
- Your model compiled and the app ran for months with the ambiguous pair; what does fixing it involve?Add the explicit inverses, then repair existing data: the one-way relationships may disagree, so walk the tasks and rebuild each user's arrays from whichever side the app actually wrote. The inverse is part of the schema, so test the upgrade by opening a store created by the shipped version.
- Does it matter which side carries the inverse: argument?No. Either end of a pairing can declare it; Apple's samples put it on the to-many side in one place and on the to-one side in another. What matters is that each pairing is declared exactly once.
- SwiftData always works out which property pairs with which.
- Put @Relationship(inverse:) on both ends of the link so each side knows the other.
- If the inverse were missing, the container would fail to open.
- Every relationship property must have an inverse.
- The inverse argument may only be written on the to-many side.