How to create one-to-many relationships
How to create one-to-many relationships 관련
Updated for Xcode 15
One-to-many relationships are the most common relationship type in SwiftData, and are created automatically when one side of your relationship has an array of data, and the other side is optional.
For example, we might say that every movie has a director, and each director can have directed many movies:
@Model
class Movie {
var name: String
var releaseYear: Int
var director: Director?
init(name: String, releaseYear: Int, director: Director) {
self.name = name
self.releaseYear = releaseYear
self.director = director
}
}
@Model
class Director {
var name: String
var movies: [Movie]
init(name: String, movies: [Movie]) {
self.name = name
self.movies = movies
}
}
If you prefer to make the relationship explicit – and I usually recommend doing so, not least because it helps clarify your intent – you’re specify exactly what the inverse should be, and also what happens when a deletion takes place.
So, we might adjust the Director
definition to this, to say that when we delete a director we replace them with nil in any movie they directed:
@Relationship(deleteRule: .nullify, inverse: \Movie.director) var movies: [Movie]
Important
There are a handful of rules you need to follow with these relationships:
- If you intend to use inferred relationships, one side of your data must be optional.
- If you use an explicit relationship where one side of your data is non-optional, be careful how you delete objects – SwiftData uses the
.nullify
delete rule by default, which can put your data into in an invalid state. To avoid this problem, either use an option value, or use a.cascade
delete rule. - Do not attempt to use collection types other than
Array
, because your code will simply not compile.