# 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)

*** ** * ** ***

Common/AllAndroid/JVMNative/C/iOSJavaScript
- Cmn

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

*** ** * ** ***

Marks a property in an `https://developer.android.com/reference/kotlin/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/kotlin/androidx/room3/Entity#primaryKeys()` function.

Each `https://developer.android.com/reference/kotlin/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/kotlin/androidx/room3/Entity` and its super class defines a `https://developer.android.com/reference/kotlin/androidx/room3/PrimaryKey`, the child's `https://developer.android.com/reference/kotlin/androidx/room3/PrimaryKey` definition will override the parent's `https://developer.android.com/reference/kotlin/androidx/room3/PrimaryKey`.

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

## Summary

| ### Nested types |
|---|
| `enum https://developer.android.com/reference/kotlin/androidx/room3/PrimaryKey.Algorithm : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-enum/index.html<https://developer.android.com/reference/kotlin/androidx/room3/PrimaryKey.Algorithm>` The `https://developer.android.com/reference/kotlin/androidx/room3/PrimaryKey` auto-generate algorithms. |

| ### Public constructors |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/room3/PrimaryKey#PrimaryKey(kotlin.Boolean,androidx.room3.PrimaryKey.Algorithm)(autoGenerate: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html, algorithm: https://developer.android.com/reference/kotlin/androidx/room3/PrimaryKey.Algorithm)` | Cmn |

| ### Public properties |
|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/room3/PrimaryKey.Algorithm` | `https://developer.android.com/reference/kotlin/androidx/room3/PrimaryKey#algorithm()` When `https://developer.android.com/reference/kotlin/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. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/room3/PrimaryKey#autoGenerate()` Set to true to let SQLite generate the unique id. | Cmn |

## Public constructors

### PrimaryKey

Cmn Added in [3.0.0](https://developer.android.com/jetpack/androidx/releases/room3#3.0.0)

```
PrimaryKey(
    autoGenerate: Boolean = false,
    algorithm: PrimaryKey.Algorithm = Algorithm.AUTOINCREMENT
)
```

## Public properties

### algorithm

Cmn

```
val algorithm: PrimaryKey.Algorithm
```

When `https://developer.android.com/reference/kotlin/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/kotlin/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/kotlin/androidx/room3/PrimaryKey.Algorithm` | The algorithm to use for auto-generation. Defaults to `https://developer.android.com/reference/kotlin/androidx/room3/PrimaryKey.Algorithm#AUTOINCREMENT`. |

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

### autoGenerate

Cmn

```
val autoGenerate: Boolean
```

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/kotlin/androidx/room3/ColumnTypeConverter` converts it to a `Long` or `Int`), `https://developer.android.com/reference/kotlin/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/kotlin/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/kotlin/androidx/room3/PrimaryKey#(kotlin.Boolean,androidx.room3.PrimaryKey.Algorithm)` property.

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | Whether the primary key should be auto-generated by SQLite or not. Defaults to false. |

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