# PrimaryKey

Artifact: [androidx.room3:room3-common](https://developer.android.com/jetpack/androidx/releases/room3) [View Source](https://cs.android.com/search?q=file:androidx/room3/PrimaryKey.kt+class:androidx.room3.PrimaryKey) Added in [3.0.0](https://developer.android.com/jetpack/androidx/releases/room3#3.0.0)

*** ** * ** ***


```
@Target(allowedTargets = [AnnotationTarget.PROPERTY, AnnotationTarget.FIELD, AnnotationTarget.FUNCTION])
@Retention(value = AnnotationRetention.BINARY)
public annotation PrimaryKey
```

<br />

*** ** * ** ***

Marks a property in an `https://developer.android.com/reference/androidx/room3/Entity` as the primary key.

If you would like to define a composite primary key, you should use `https://developer.android.com/reference/androidx/room3/Entity#primaryKeys()` function.

Each `https://developer.android.com/reference/androidx/room3/Entity` must declare a primary key unless one of its super classes declares a primary key. If both an `https://developer.android.com/reference/androidx/room3/Entity` and its super class defines a `https://developer.android.com/reference/androidx/room3/PrimaryKey`, the child's `https://developer.android.com/reference/androidx/room3/PrimaryKey` definition will override the parent's `https://developer.android.com/reference/androidx/room3/PrimaryKey`.

If this annotation is used on a `https://developer.android.com/reference/androidx/room3/Embedded` property, all columns inherited from that embedded property becomes the composite primary key (including its grand children properties).

## Summary

| ### Nested types |
|---|
| `public enum https://developer.android.com/reference/androidx/room3/PrimaryKey.Algorithm extends https://developer.android.com/reference/java/lang/Enum.html<@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/room3/PrimaryKey.Algorithm>` The `https://developer.android.com/reference/androidx/room3/PrimaryKey` auto-generate algorithms. |

| ### Public methods |
|---|---|
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/room3/PrimaryKey.Algorithm` | `https://developer.android.com/reference/androidx/room3/PrimaryKey#getAlgorithm()()` When `https://developer.android.com/reference/androidx/room3/PrimaryKey#(kotlin.Boolean,androidx.room3.PrimaryKey.Algorithm)` is set to `true` then the algorithm used to auto-generate primary keys can be configured through this property. |
| `final boolean` | `https://developer.android.com/reference/androidx/room3/PrimaryKey#getAutoGenerate()()` Set to true to let SQLite generate the unique id. |

## Public methods

### getAlgorithm

```
public final @NonNull PrimaryKey.Algorithm getAlgorithm()
```

When `https://developer.android.com/reference/androidx/room3/PrimaryKey#(kotlin.Boolean,androidx.room3.PrimaryKey.Algorithm)` is set to `true` then the algorithm used to auto-generate primary keys can be configured through this property. When `https://developer.android.com/reference/androidx/room3/PrimaryKey#(kotlin.Boolean,androidx.room3.PrimaryKey.Algorithm)` is set to `false` this value is ignored.

For details, see the [auto increment documentation](https://sqlite.org/autoinc.html).

| Returns |
|---|---|
| `@https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/androidx/room3/PrimaryKey.Algorithm` | The algorithm to use for auto-generation. Defaults to `https://developer.android.com/reference/androidx/room3/PrimaryKey.Algorithm#AUTOINCREMENT`. |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/room3/PrimaryKey#(kotlin.Boolean,androidx.room3.PrimaryKey.Algorithm)` |   |

### getAutoGenerate

```
public final boolean getAutoGenerate()
```

Set to true to let SQLite generate the unique id.

When set to `true`, the SQLite type affinity for the property should be `INTEGER`.

If the property type is `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` or `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` (or its `https://developer.android.com/reference/androidx/room3/ColumnTypeConverter` converts it to a `Long` or `Int`), `https://developer.android.com/reference/androidx/room3/Insert` functions treat `0` as not-set while inserting the item. Similarly, if the property type is nullable (`Long?` or `Int?`), `https://developer.android.com/reference/androidx/room3/Insert` functions treat `null` as not-set while inserting the item.

The strategy on how the id will be generated will be based on the `https://developer.android.com/reference/androidx/room3/PrimaryKey#(kotlin.Boolean,androidx.room3.PrimaryKey.Algorithm)` property.

| Returns |
|---|---|
| `boolean` | Whether the primary key should be auto-generated by SQLite or not. Defaults to false. |

| See also |
|---|---|
| `https://developer.android.com/reference/androidx/room3/PrimaryKey#(kotlin.Boolean,androidx.room3.PrimaryKey.Algorithm)` |   |