If you would like to define a composite primary key, you should use primaryKeys()
method.
Each Entity must declare a primary key unless one of its super classes declares a
primary key. If both an Entity and its super class defines a PrimaryKey, the
child's PrimaryKey definition will override the parent's PrimaryKey.
If PrimaryKey annotation is used on a Embeddedd field, all columns inherited
from that embedded field becomes the composite primary key (including its grand children
fields).
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), Insert methods treat 0 as not-set while
inserting the item.
If the field's type is Integer or Long (or its TypeConverter converts it to
an Integer or a Long), 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.
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-02-10 UTC."],[],[],null,["# PrimaryKey\n\nSummary: [Methods](#pubmethods) \\| [Inherited Methods](#inhmethods) \n\nPrimaryKey\n==========\n\n| The `android.arch` Architecture Components packages are no longer maintained. They have been superseded by the corresponding [androidx.\\*](/jetpack/androidx/migrate) packages. See [androidx.room.PrimaryKey](/reference/androidx/room/PrimaryKey) instead.\n\n\n`\npublic\n\n\nabstract\n@interface\nPrimaryKey\n`\n\n\n`\n\n\nimplements\n\nAnnotation\n\n\n`\n\n|------------------------------------------|\n| android.arch.persistence.room.PrimaryKey |\n\n\u003cbr /\u003e\n\n*** ** * ** ***\n\nMarks a field in an [Entity](/reference/android/arch/persistence/room/Entity) as the primary key.\n\n\nIf you would like to define a composite primary key, you should use [primaryKeys()](/reference/android/arch/persistence/room/Entity#primaryKeys())\nmethod.\n\n\nEach [Entity](/reference/android/arch/persistence/room/Entity) must declare a primary key unless one of its super classes declares a\nprimary key. If both an [Entity](/reference/android/arch/persistence/room/Entity) and its super class defines a `PrimaryKey`, the\nchild's `PrimaryKey` definition will override the parent's `PrimaryKey`.\n\n\nIf `PrimaryKey` annotation is used on a [Embedded](/reference/android/arch/persistence/room/Embedded)d field, all columns inherited\nfrom that embedded field becomes the composite primary key (including its grand children\nfields).\n\nSummary\n-------\n\n| ### Public methods ||\n|------------|----------------------------------------------------------------------------------------------------------------------------------------------|\n| ` boolean` | ` `[autoGenerate](/reference/android/arch/persistence/room/PrimaryKey#autoGenerate())`() ` Set to true to let SQLite generate the unique id. |\n\n| ### Inherited methods |\n|-----------------------|---|\n| From interface ` java.lang.annotation.Annotation ` |-----------------------------------------|-------------------------| | ` abstract Class\u003c? extends Annotation\u003e` | ` annotationType() ` | | ` abstract boolean` | ` equals(Object arg0) ` | | ` abstract int` | ` hashCode() ` | | ` abstract String` | ` toString() ` | ||\n\nPublic methods\n--------------\n\n### autoGenerate\n\n```\nboolean autoGenerate ()\n```\n\nSet to true to let SQLite generate the unique id.\n\n\nWhen set to `true`, the SQLite type affinity for the field should be `INTEGER`.\n\n\nIf the field type is `long` or `int` (or its TypeConverter converts it to a\n`long` or `int`), [Insert](/reference/android/arch/persistence/room/Insert) methods treat `0` as not-set while\ninserting the item.\n\n\nIf the field's type is [Integer](/reference/java/lang/Integer) or [Long](/reference/java/lang/Long) (or its TypeConverter converts it to\nan [Integer](/reference/java/lang/Integer) or a [Long](/reference/java/lang/Long)), [Insert](/reference/android/arch/persistence/room/Insert) methods treat `null` as\nnot-set while inserting the item.\n\n\u003cbr /\u003e\n\n| Returns ||\n|-----------|----------------------------------------------------------------------------------------------|\n| `boolean` | Whether the primary key should be auto-generated by SQLite or not. Defaults to false. \u003cbr /\u003e |\n\n-\n\n Annotations\n -----------\n\n - [ColumnInfo](/reference/android/arch/persistence/room/ColumnInfo)\n - [ColumnInfo.Collate](/reference/android/arch/persistence/room/ColumnInfo.Collate)\n - [ColumnInfo.SQLiteTypeAffinity](/reference/android/arch/persistence/room/ColumnInfo.SQLiteTypeAffinity)\n - [Dao](/reference/android/arch/persistence/room/Dao)\n - [Database](/reference/android/arch/persistence/room/Database)\n - [Delete](/reference/android/arch/persistence/room/Delete)\n - [Embedded](/reference/android/arch/persistence/room/Embedded)\n - [Entity](/reference/android/arch/persistence/room/Entity)\n - [ForeignKey](/reference/android/arch/persistence/room/ForeignKey)\n - [ForeignKey.Action](/reference/android/arch/persistence/room/ForeignKey.Action)\n - [Ignore](/reference/android/arch/persistence/room/Ignore)\n - [Index](/reference/android/arch/persistence/room)\n - [Insert](/reference/android/arch/persistence/room/Insert)\n - [OnConflictStrategy](/reference/android/arch/persistence/room/OnConflictStrategy)\n - [PrimaryKey](/reference/android/arch/persistence/room/PrimaryKey)\n - [Query](/reference/android/arch/persistence/room/Query)\n - [RawQuery](/reference/android/arch/persistence/room/RawQuery)\n - [Relation](/reference/android/arch/persistence/room/Relation)\n - [SkipQueryVerification](/reference/android/arch/persistence/room/SkipQueryVerification)\n - [Transaction](/reference/android/arch/persistence/room/Transaction)\n - [TypeConverter](/reference/android/arch/persistence/room/TypeConverter)\n - [TypeConverters](/reference/android/arch/persistence/room/TypeConverters)\n - [Update](/reference/android/arch/persistence/room/Update)\n-\n\n Classes\n -------\n\n - [DatabaseConfiguration](/reference/android/arch/persistence/room/DatabaseConfiguration)\n - [InvalidationTracker](/reference/android/arch/persistence/room/InvalidationTracker)\n - [InvalidationTracker.Observer](/reference/android/arch/persistence/room/InvalidationTracker.Observer)\n - [Room](/reference/android/arch/persistence/room/Room)\n - [RoomDatabase](/reference/android/arch/persistence/room/RoomDatabase)\n - [RoomDatabase.Builder](/reference/android/arch/persistence/room/RoomDatabase.Builder)\n - [RoomDatabase.Callback](/reference/android/arch/persistence/room/RoomDatabase.Callback)\n - [RoomDatabase.MigrationContainer](/reference/android/arch/persistence/room/RoomDatabase.MigrationContainer)\n - [RoomWarnings](/reference/android/arch/persistence/room/RoomWarnings)\n - [RxRoom](/reference/android/arch/persistence/room/RxRoom)\n-\n\n Enums\n -----\n\n - [RoomDatabase.JournalMode](/reference/android/arch/persistence/room/RoomDatabase.JournalMode)\n-\n\n Exceptions\n ----------\n\n - [EmptyResultSetException](/reference/android/arch/persistence/room/EmptyResultSetException)"]]