<!--
{
  "availability" : [
    "iOS: 17.0.0 -",
    "iPadOS: 17.0.0 -",
    "macCatalyst: 17.0.0 -",
    "macOS: 14.0.0 -",
    "swift: 5.9.0 -",
    "tvOS: 17.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 10.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftData",
  "identifier" : "/documentation/SwiftData/Relationship(_:deleteRule:minimumModelCount:maximumModelCount:originalName:inverse:hashModifier:)",
  "metadataVersion" : "0.1.0",
  "role" : "Macro",
  "symbol" : {
    "kind" : "Macro",
    "modules" : [
      "SwiftData"
    ],
    "preciseIdentifier" : "s:9SwiftData12Relationship_10deleteRule17minimumModelCount07maximumgH012originalName7inverse12hashModifieryAA6SchemaCABC6OptionVd_AK06DeleteE0OSiSgAPSSSgs10AnyKeyPathCSgAQtcfm"
  },
  "title" : "Relationship(_:deleteRule:minimumModelCount:maximumModelCount:originalName:inverse:hashModifier:)"
}
-->

# Relationship(_:deleteRule:minimumModelCount:maximumModelCount:originalName:inverse:hashModifier:)

Specifies the options that SwiftData needs to manage the annotated property as
a relationship between two models.

```
@attached(peer) macro Relationship(_ options: Schema.Relationship.Option..., deleteRule: Schema.Relationship.DeleteRule = .nullify, minimumModelCount: Int? = 0, maximumModelCount: Int? = 0, originalName: String? = nil, inverse: AnyKeyPath? = nil, hashModifier: String? = nil)
```

## Parameters

`options`

A list of options to apply to the annotated property to customize its behavior. For possible values, see [`Schema.Relationship.Option`](/documentation/SwiftData/Schema/Relationship/Option).

`deleteRule`

The rule to apply when you delete the relationship’s owning persistent model. For possible values, see [`Schema.Relationship.DeleteRule`](/documentation/SwiftData/Schema/Relationship/DeleteRule-swift.enum). The default value is [`Schema.Relationship.DeleteRule.nullify`](/documentation/SwiftData/Schema/Relationship/DeleteRule-swift.enum/nullify).

`minimumModelCount`

The minimum number of models the relationship can reference. The default value is `0`.

`maximumModelCount`

The maximum number of models the relationship can reference. The default value is `0`.

`originalName`

The previous name of the attribute, if it’s different to the one in the current schema version. The default value is `nil`.

`inverse`

The key path of the relationship that represents the inverse of this relationship. The default value is `nil`.

`hashModifier`

A unique hash value that represents the most recent version of the annotated property. The default value is `nil`.

## Overview

If one or more of a model’s properties represent relationships between their
containing model and another model, annotate those properties with the
`@Relationship` macro. This enables SwiftData to enforce those relationships at
runtime — including what happens if you delete related data – as well as write
any associated metadata to the persistent storage so the relationships exist
across app launches.

In the following example, a remote image may belong to a category, and a
category can contain zero, one, or more images.

```swift
@Model
class RemoteImage {
    @Attribute(.unique) var sourceURL: URL
    @Relationship(inverse: \Category.images) var category: Category?
    var data: Data

    init(sourceURL: URL, data: Data = Data()) {
        self.sourceURL = sourceURL
        self.data = data
    }
}

@Model
class Category {
    @Attribute(.unique) var name: String
    @Relationship var images = [RemoteImage]()

    init(name: String) {
        self.name = name
    }
}
```

> Note: If you declare a relationship attribute as optional when defining your
> persistent models, SwiftData only enforces `minimumModelCount` and
> `maximumModelCount` when that attribute isn’t nil.

For more information about defining relationships between models, see
[Defining data relationships with enumerations and model classes](/documentation/SwiftData/Defining-data-relationships-with-enumerations-and-model-classes).

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)