---
title: "Unique(_:)"
framework: swiftdata
role: symbol
role_heading: Macro
path: "swiftdata/unique(_:)"
---

# Unique(_:)

Specifies the key-paths that SwiftData uses to enforce the uniqueness of model instances.

## Declaration

```swift
@freestanding(declaration) macro Unique<T>(_ constraints: [PartialKeyPath<T>]...) where T : PersistentModel
```

## Parameters

- `constraints`: Arrays of model key-paths that form the unique constraints to apply to the enclosing model.

## Overview

Overview If a model class contains attributes that you require to be unique across all persisted instances of that model, add the Unique macro to that model’s definition. You can specify a constraint on a single attribute, a compound constraint across multiple attributes, or any combination of the two. important: For relationship attributes, SwiftData only supports unique constraints on those that reference a single persistent model, rather than an array of persistent models. The following example declares that every instance of Person has a unique id, and that no two instances of Person have the same givenName and familyName: @Model final class Person {     // Declare any unique constraints as part of the model definition.     #Unique<Person>([\.id], [\.givenName, \.familyName])

var id: UUID     var givenName: String     var familyName: String

init(id: UUID, givenName: String, familyName: String) {         self.id = id         self.givenName = givenName         self.familyName = familyName     } }

## See Also

### Model definition

- [Model()](swiftdata/model().md)
- [Attribute(_:originalName:hashModifier:)](swiftdata/attribute(_:originalname:hashmodifier:).md)
- [Index(_:)](swiftdata/index(_:)-74ia2.md)
- [Index(_:)](swiftdata/index(_:)-7d4z0.md)
- [Defining data relationships with enumerations and model classes](swiftdata/defining-data-relationships-with-enumerations-and-model-classes.md)
- [Relationship(_:deleteRule:minimumModelCount:maximumModelCount:originalName:inverse:hashModifier:)](swiftdata/relationship(_:deleterule:minimummodelcount:maximummodelcount:originalname:inverse:hashmodifier:).md)
- [Transient()](swiftdata/transient().md)
