How to define one-to-many relationships in SwiftData
Learn to use SwiftData's @Relationship macro.
updated on 13 Jul 2026 · 4 min read
In this article, we will look at how to define one-to-many relationships in SwiftData. If you are new to SwiftData, check out this developer guide on SwiftData first.
Let's directly jump in.

Relationships in SwiftData can only be defined between reference types, i.e. classes. Let's look at an example of a one-to-many relationship:
@Modelclass User {@Relationship(inverse: \Note.user) var notes: [Note]?init(notes: [Note]? = nil) {self.notes = notes}}@Modelclass Note {var user: Uservar title: Stringvar text: Stringinit(user: User, title: String, text: String) {self.user = userself.title = titleself.text = text}}
In the example above, we use the @Relationship macro to define a one-to-many relationship between a user and their notes. With that in place, if we now create a new Note for example, this note will automatically be added to the user's list of notes.
An interesting fact to know here is that when both relationship ends are declared as optional, SwiftData will automatically infer an inverse relationship even without the @Relationship macro.
Furthermore, we can use the @Relationship macro to specify delete rules. By default, SwiftData uses the .nullify delete rule which only nullifies the related model's reference to the deleted model. That means that if in our example above a User gets deleted, then their notes will stay intact in our data store. In some cases that's the behaviour we want, but in our example, we'd like to delete the user's notes when the user gets deleted. For that, we can use the .cascade delete rule:
@Modelclass User {@Relationship(deleteRule: .cascade) var notes: [Note]?}
With a .cascade rule, all user related notes will be deleted whenever the user is deleted.



Newsletter
Like to support my work?
Say hi
Related tags
Articles with related topics
Latest articles and tips



