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

*** ** * ** ***


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

<br />

*** ** * ** ***

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

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

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

## Summary

| ### Public methods |
|---|---|
| `final boolean` | `https://developer.android.com/reference/androidx/room/PrimaryKey#getAutoGenerate()()` Set to true to let SQLite generate the unique id. |

## Public methods

### 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 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/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/androidx/room/Insert` methods treat `null` as not-set while inserting the item.

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