# PrimaryKey

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

*** ** * ** ***

Common/AllAndroid/JVMNative/C/iOSJavaScript
- Cmn

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

*** ** * ** ***

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

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

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

## Summary

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

| ### Public properties |
|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/room/PrimaryKey#autoGenerate()` Set to true to let SQLite generate the unique id. | Cmn |

## Public constructors

### PrimaryKey

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

```
PrimaryKey(autoGenerate: Boolean = false)
```

## Public properties

### 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 field should be `INTEGER`.

If the field type is `Long` or `Int` (or its TypeConverter converts it to a `Long` or `Int`), `https://developer.android.com/reference/kotlin/androidx/room/Insert` methods treat `0` as not-set while inserting the item.

If the field's type is `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` or `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html` (or its TypeConverter converts it to an `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` or `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html`), `https://developer.android.com/reference/kotlin/androidx/room/Insert` methods treat `null` as not-set while inserting the item.

| 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. |