# Relationship

    @propertyWrapper
    public struct Relationship<V> : RelationshipKeyPathStringConvertible, FieldRelationshipProtocol where V : FieldRelationshipType

    extension FieldContainer.Relationship: ToManyRelationshipKeyPathStringConvertible where V: FieldRelationshipToManyType

The containing type for relationships. Any `CoreStoreObject` subclass can be a destination type. Inverse relationships should be declared from the destination type as well, using the `inverse:` argument for the relationship.

    class Dog: CoreStoreObject {

        @Field.Relationship("master")
        var master: Person?
    }

    class Person: CoreStoreObject {

        @Field.Relationship("pets", inverse: \.$master)
        var pets: Set<Dog>
    }

Important

`Field` properties are required to be used as `@propertyWrapper`s. Any other declaration not using the `@Field.Relationship(...) var` syntax will be ignored.

### AnyKeyPathStringConvertible

- `cs_keyPathString`

  #### Declaration

  Swift

      public var cs_keyPathString: String { get }

### KeyPathStringConvertible

- `ObjectType`

  #### Declaration

  Swift

      public typealias ObjectType = O

- `DestinationValueType`

  #### Declaration

  Swift

      public typealias DestinationValueType = V.DestinationObjectType

### RelationshipKeyPathStringConvertible

- `ReturnValueType`

  #### Declaration

  Swift

      public typealias ReturnValueType = V

### DeleteRule

- `DeleteRule`

  These constants define what happens to relationships when an object is deleted.

  See more

  #### Declaration

  Swift

      public enum DeleteRule

### Available where `V`: `FieldRelationshipToOneType`

- `init(_:deleteRule:versionHashModifier:previousVersionKeyPath:affectedByKeyPaths:)`

  Initializes the metadata for the relationship. All relationships require an “inverse”, so updates to to this object’s relationship are also reflected on its destination object. Make sure to declare this relationship’s inverse relationship on its destination object. Due to Swift’s compiler limitation, only one of the relationship and its inverse can declare an `inverse:` argument.

       class Dog: CoreStoreObject {

           @Field.Relationship("master")
           var master: Person?
       }

       class Person: CoreStoreObject {

           @Field.Relationship("pets", inverse: \.$master)
           var pets: Set<Dog>
       }

  #### Declaration

  Swift

      public init(
          _ keyPath: KeyPathString,
          deleteRule: DeleteRule = .nullify,
          versionHashModifier: @autoclosure @escaping () -> String? = nil,
          previousVersionKeyPath: @autoclosure @escaping () -> String? = nil,
          affectedByKeyPaths: @autoclosure @escaping () -> Set<KeyPathString> = []
      )

  #### Parameters

  `keyPath` | the permanent name for this relationship.
  `deleteRule` | defines what happens to relationship when an object is deleted. Valid values are `.nullify`, `.cascade`, and `.delete`. Defaults to `.nullify`.
  `versionHashModifier` | used to mark or denote a property as being a different “version” than another even if all of the values which affect persistence are equal. (Such a difference is important in cases where the properties are unchanged but the format or content of its data are changed.)
  `previousVersionKeyPath` | used to resolve naming conflicts between models. When creating an entity mapping between entities in two managed object models, a source entity property’s `keyPath` with a matching destination entity property’s `previousVersionKeyPath` indicate that a property mapping should be configured to migrate from the source to the destination. If unset, the identifier will be the property’s `keyPath`.
  `affectedByKeyPaths` | a set of key paths for properties whose values affect the value of the receiver. This is similar to `NSManagedObject.keyPathsForValuesAffectingValue(forKey:)`.

- `init(_:inverse:deleteRule:versionHashModifier:previousVersionKeyPath:affectedByKeyPaths:)`

  Initializes the metadata for the relationship. All relationships require an “inverse”, so updates to to this object’s relationship are also reflected on its destination object. Make sure to declare this relationship’s inverse relationship on its destination object. Due to Swift’s compiler limitation, only one of the relationship and its inverse can declare an `inverse:` argument.

       class Dog: CoreStoreObject {

           @Field.Relationship("master")
           var master: Person?
       }

       class Person: CoreStoreObject {

           @Field.Relationship("pets", inverse: \.$master)
           var pets: Set<Dog>
       }

  #### Declaration

  Swift

      public init<D>(
          _ keyPath: KeyPathString,
          inverse: KeyPath<V.DestinationObjectType, FieldContainer<V.DestinationObjectType>.Relationship<D>>,
          deleteRule: DeleteRule = .nullify,
          versionHashModifier: @autoclosure @escaping () -> String? = nil,
          previousVersionKeyPath: @autoclosure @escaping () -> String? = nil,
          affectedByKeyPaths: @autoclosure @escaping () -> Set<KeyPathString> = []
      ) where D: FieldRelationshipType

  #### Parameters

  `keyPath` | the permanent name for this relationship.
  `inverse` | the inverse relationship that is declared for the destination object. All relationships require an “inverse”, so updates to to this object’s relationship are also reflected on its destination object.
  `deleteRule` | defines what happens to relationship when an object is deleted. Valid values are `.nullify`, `.cascade`, and `.delete`. Defaults to `.nullify`.
  `versionHashModifier` | used to mark or denote a property as being a different “version” than another even if all of the values which affect persistence are equal. (Such a difference is important in cases where the properties are unchanged but the format or content of its data are changed.)
  `previousVersionKeyPath` | used to resolve naming conflicts between models. When creating an entity mapping between entities in two managed object models, a source entity property’s `keyPath` with a matching destination entity property’s `previousVersionKeyPath` indicate that a property mapping should be configured to migrate from the source to the destination. If unset, the identifier will be the property’s `keyPath`.
  `affectedByKeyPaths` | a set of key paths for properties whose values affect the value of the receiver. This is similar to `NSManagedObject.keyPathsForValuesAffectingValue(forKey:)`.

### Available where `V`: `FieldRelationshipToManyOrderedType`

- `init(_:minCount:maxCount:deleteRule:versionHashModifier:previousVersionKeyPath:affectedByKeyPaths:)`

  Initializes the metadata for the relationship. All relationships require an “inverse”, so updates to to this object’s relationship are also reflected on its destination object. Make sure to declare this relationship’s inverse relationship on its destination object. Due to Swift’s compiler limitation, only one of the relationship and its inverse can declare an `inverse:` argument.

       class Dog: CoreStoreObject {

           @Field.Relationship("master")
           var master: Person?
       }

       class Person: CoreStoreObject {

           @Field.Relationship("pets", inverse: \.$master)
           var pets: Array<Dog>
       }

  #### Declaration

  Swift

      public init(
          _ keyPath: KeyPathString,
          minCount: Int = 0,
          maxCount: Int = 0,
          deleteRule: DeleteRule = .nullify,
          versionHashModifier: @autoclosure @escaping () -> String? = nil,
          previousVersionKeyPath: @autoclosure @escaping () -> String? = nil,
          affectedByKeyPaths: @autoclosure @escaping () -> Set<KeyPathString> = []
      )

  #### Parameters

  `keyPath` | the permanent name for this relationship.
  `minCount` | the minimum number of objects in this relationship UNLESS THE RELATIONSHIP IS EMPTY. This means there might be zero objects in the relationship, which might be less than `minCount`. If the number of objects in the relationship do not satisfy `minCount` and `maxCount`, the transaction’s commit (or auto-commit) would fail with a validation error.
  `maxCount` | the maximum number of objects in this relationship. If the number of objects in the relationship do not satisfy `minCount` and `maxCount`, the transaction’s commit (or auto-commit) would fail with a validation error.
  `deleteRule` | defines what happens to relationship when an object is deleted. Valid values are `.nullify`, `.cascade`, and `.delete`. Defaults to `.nullify`.
  `versionHashModifier` | used to mark or denote a property as being a different “version” than another even if all of the values which affect persistence are equal. (Such a difference is important in cases where the properties are unchanged but the format or content of its data are changed.)
  `previousVersionKeyPath` | used to resolve naming conflicts between models. When creating an entity mapping between entities in two managed object models, a source entity property’s `keyPath` with a matching destination entity property’s `previousVersionKeyPath` indicate that a property mapping should be configured to migrate from the source to the destination. If unset, the identifier will be the property’s `keyPath`.
  `affectedByKeyPaths` | a set of key paths for properties whose values affect the value of the receiver. This is similar to `NSManagedObject.keyPathsForValuesAffectingValue(forKey:)`.

- `init(_:minCount:maxCount:inverse:deleteRule:versionHashModifier:previousVersionKeyPath:affectedByKeyPaths:)`

  Initializes the metadata for the relationship. All relationships require an “inverse”, so updates to to this object’s relationship are also reflected on its destination object. Make sure to declare this relationship’s inverse relationship on its destination object. Due to Swift’s compiler limitation, only one of the relationship and its inverse can declare an `inverse:` argument.

       class Dog: CoreStoreObject {

           @Field.Relationship("master")
           var master: Person?
       }

       class Person: CoreStoreObject {

           @Field.Relationship("pets", inverse: \.$master)
           var pets: Array<Dog>
       }

  #### Declaration

  Swift

      public init<D>(
          _ keyPath: KeyPathString,
          minCount: Int = 0,
          maxCount: Int = 0,
          inverse: KeyPath<V.DestinationObjectType, FieldContainer<V.DestinationObjectType>.Relationship<D>>,
          deleteRule: DeleteRule = .nullify,
          versionHashModifier: @autoclosure @escaping () -> String? = nil,
          previousVersionKeyPath: @autoclosure @escaping () -> String? = nil,
          affectedByKeyPaths: @autoclosure @escaping () -> Set<KeyPathString> = []
      ) where D: FieldRelationshipType

  #### Parameters

  `keyPath` | the permanent name for this relationship.
  `minCount` | the minimum number of objects in this relationship UNLESS THE RELATIONSHIP IS EMPTY. This means there might be zero objects in the relationship, which might be less than `minCount`. If the number of objects in the relationship do not satisfy `minCount` and `maxCount`, the transaction’s commit (or auto-commit) would fail with a validation error.
  `maxCount` | the maximum number of objects in this relationship. If the number of objects in the relationship do not satisfy `minCount` and `maxCount`, the transaction’s commit (or auto-commit) would fail with a validation error.
  `inverse` | the inverse relationship that is declared for the destination object. All relationships require an “inverse”, so updates to to this object’s relationship are also reflected on its destination object.
  `deleteRule` | defines what happens to relationship when an object is deleted. Valid values are `.nullify`, `.cascade`, and `.delete`. Defaults to `.nullify`.
  `versionHashModifier` | used to mark or denote a property as being a different “version” than another even if all of the values which affect persistence are equal. (Such a difference is important in cases where the properties are unchanged but the format or content of its data are changed.)
  `previousVersionKeyPath` | used to resolve naming conflicts between models. When creating an entity mapping between entities in two managed object models, a source entity property’s `keyPath` with a matching destination entity property’s `previousVersionKeyPath` indicate that a property mapping should be configured to migrate from the source to the destination. If unset, the identifier will be the property’s `keyPath`.
  `affectedByKeyPaths` | a set of key paths for properties whose values affect the value of the receiver. This is similar to `NSManagedObject.keyPathsForValuesAffectingValue(forKey:)`.

### Available where `V`: `FieldRelationshipToManyUnorderedType`

- `init(_:minCount:maxCount:deleteRule:versionHashModifier:previousVersionKeyPath:affectedByKeyPaths:)`

  Initializes the metadata for the relationship. All relationships require an “inverse”, so updates to to this object’s relationship are also reflected on its destination object. Make sure to declare this relationship’s inverse relationship on its destination object. Due to Swift’s compiler limitation, only one of the relationship and its inverse can declare an `inverse:` argument.

       class Dog: CoreStoreObject {

           @Field.Relationship("master")
           var master: Person?
       }

       class Person: CoreStoreObject {

           @Field.Relationship("pets", inverse: \.$master)
           var pets: Set<Dog>
       }

  #### Declaration

  Swift

      public init(
          _ keyPath: KeyPathString,
          minCount: Int = 0,
          maxCount: Int = 0,
          deleteRule: DeleteRule = .nullify,
          versionHashModifier: @autoclosure @escaping () -> String? = nil,
          previousVersionKeyPath: @autoclosure @escaping () -> String? = nil,
          affectedByKeyPaths: @autoclosure @escaping () -> Set<KeyPathString> = []
      )

  #### Parameters

  `keyPath` | the permanent name for this relationship.
  `minCount` | the minimum number of objects in this relationship UNLESS THE RELATIONSHIP IS EMPTY. This means there might be zero objects in the relationship, which might be less than `minCount`. If the number of objects in the relationship do not satisfy `minCount` and `maxCount`, the transaction’s commit (or auto-commit) would fail with a validation error.
  `maxCount` | the maximum number of objects in this relationship. If the number of objects in the relationship do not satisfy `minCount` and `maxCount`, the transaction’s commit (or auto-commit) would fail with a validation error.
  `deleteRule` | defines what happens to relationship when an object is deleted. Valid values are `.nullify`, `.cascade`, and `.delete`. Defaults to `.nullify`.
  `versionHashModifier` | used to mark or denote a property as being a different “version” than another even if all of the values which affect persistence are equal. (Such a difference is important in cases where the properties are unchanged but the format or content of its data are changed.)
  `previousVersionKeyPath` | used to resolve naming conflicts between models. When creating an entity mapping between entities in two managed object models, a source entity property’s `keyPath` with a matching destination entity property’s `previousVersionKeyPath` indicate that a property mapping should be configured to migrate from the source to the destination. If unset, the identifier will be the property’s `keyPath`.
  `affectedByKeyPaths` | a set of key paths for properties whose values affect the value of the receiver. This is similar to `NSManagedObject.keyPathsForValuesAffectingValue(forKey:)`.

- `init(_:minCount:maxCount:inverse:deleteRule:versionHashModifier:previousVersionKeyPath:affectedByKeyPaths:)`

  Initializes the metadata for the relationship. All relationships require an “inverse”, so updates to to this object’s relationship are also reflected on its destination object. Make sure to declare this relationship’s inverse relationship on its destination object. Due to Swift’s compiler limitation, only one of the relationship and its inverse can declare an `inverse:` argument.

       class Dog: CoreStoreObject {

           @Field.Relationship("master")
           var master: Person?
       }

       class Person: CoreStoreObject {

           @Field.Relationship("pets", inverse: \.$master)
           var pets: Set<Dog>
       }

  #### Declaration

  Swift

      public init<D>(
          _ keyPath: KeyPathString,
          minCount: Int = 0,
          maxCount: Int = 0,
          inverse: KeyPath<V.DestinationObjectType, FieldContainer<V.DestinationObjectType>.Relationship<D>>,
          deleteRule: DeleteRule = .nullify,
          versionHashModifier: @autoclosure @escaping () -> String? = nil,
          previousVersionKeyPath: @autoclosure @escaping () -> String? = nil,
          affectedByKeyPaths: @autoclosure @escaping () -> Set<KeyPathString> = []
      ) where D: FieldRelationshipType

  #### Parameters

  `keyPath` | the permanent name for this relationship.
  `minCount` | the minimum number of objects in this relationship UNLESS THE RELATIONSHIP IS EMPTY. This means there might be zero objects in the relationship, which might be less than `minCount`. If the number of objects in the relationship do not satisfy `minCount` and `maxCount`, the transaction’s commit (or auto-commit) would fail with a validation error.
  `maxCount` | the maximum number of objects in this relationship. If the number of objects in the relationship do not satisfy `minCount` and `maxCount`, the transaction’s commit (or auto-commit) would fail with a validation error.
  `inverse` | the inverse relationship that is declared for the destination object. All relationships require an “inverse”, so updates to to this object’s relationship are also reflected on its destination object.
  `deleteRule` | defines what happens to relationship when an object is deleted. Valid values are `.nullify`, `.cascade`, and `.delete`. Defaults to `.nullify`.
  `versionHashModifier` | used to mark or denote a property as being a different “version” than another even if all of the values which affect persistence are equal. (Such a difference is important in cases where the properties are unchanged but the format or content of its data are changed.)
  `previousVersionKeyPath` | used to resolve naming conflicts between models. When creating an entity mapping between entities in two managed object models, a source entity property’s `keyPath` with a matching destination entity property’s `previousVersionKeyPath` indicate that a property mapping should be configured to migrate from the source to the destination. If unset, the identifier will be the property’s `keyPath`.
  `affectedByKeyPaths` | a set of key paths for properties whose values affect the value of the receiver. This is similar to `NSManagedObject.keyPathsForValuesAffectingValue(forKey:)`.