Logo for tanaschita.com

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.

Sponsorship logo
Architecture & Design Patterns for iOS
This book is a practical guide to essential architectural principles and design patterns for iOS development. It covers strategies for building maintainable apps with Swift and SwiftUI, including dependency injection, navigation, common patterns, and modularization.
LEARN MORE

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:

@Model
class User {
@Relationship(inverse: \Note.user) var notes: [Note]?
init(notes: [Note]? = nil) {
self.notes = notes
}
}
@Model
class Note {
var user: User
var title: String
var text: String
init(user: User, title: String, text: String) {
self.user = user
self.title = title
self.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:

@Model
class User {
@Relationship(deleteRule: .cascade) var notes: [Note]?
}

With a .cascade rule, all user related notes will be deleted whenever the user is deleted.

Sponsorship logo
Preparing for a technical iOS job interview
Preparing for a technical iOS Job Interview with over 300 questions & answers. Covering Swift & Objective-C, SwiftUI & UIKit, Combine, HTTP Networking, iOS File System, Core Data, Concurrency with async/await, Security, Automated Testing, Dependency Management, AI & Machine Learning and more.
LEARN MORE
Sponsorship logo
Architecture & Design Patterns for iOS
This book is a practical guide to essential architectural principles and design patterns for iOS development. It covers strategies for building maintainable apps with Swift and SwiftUI, including dependency injection, navigation, common patterns, and modularization.
LEARN MORE
Sponsorship logo
Become a sponsor of tanaschita.com
By publishing an article on different iOS topics every week, tanaschita.com is constantly growing in the developer community and may provide a great audience for you as a sponsor.
CLICK TO LEARN MORE

Newsletter

Image of a reading marmot
Subscribe

Like to support my work?

Say hi

Related tags

Articles with related topics

swiftdata

architecture

swift

ios

Keeping SwiftData behind a boundary

Learn how to keep SwiftData out of SwiftUI views.

13 Jul 2026 · 5 min read

Latest articles and tips

© 2026 tanaschita.com

Privacy policy

Impressum