# ForeignKey

Summary:[Nested Classes](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#nestedclasses)\|[Constants](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#constants)\|[Methods](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#pubmethods)\|[Inherited Methods](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#inhmethods)  

# ForeignKey

| The`android.arch`Architecture Components packages are no longer maintained. They have been superseded by the corresponding[androidx.\*](https://developer.android.com/jetpack/androidx/migrate)packages. See[androidx.room.ForeignKey](https://developer.android.com/reference/androidx/room/ForeignKey)instead.

`
public


abstract
@interface
ForeignKey
`  
`


implements

Annotation


`

|------------------------------------------|
| android.arch.persistence.room.ForeignKey |

<br />

*** ** * ** ***

Declares a foreign key on another[Entity](https://developer.android.com/reference/android/arch/persistence/room/Entity).

Foreign keys allows you to specify constraints across Entities such that SQLite will ensure that the relationship is valid when you modify the database.

When a foreign key constraint is specified, SQLite requires the referenced columns to be part of a unique index in the parent table or the primary key of that table. You must create a unique index in the parent entity that covers the referenced columns (Room will verify this at compile time and print an error if it is missing).

It is also recommended to create an index on the child table to avoid full table scans when the parent table is modified. If a suitable index on the child table is missing, Room will print[MISSING_INDEX_ON_FOREIGN_KEY_CHILD](https://developer.android.com/reference/android/arch/persistence/room/RoomWarnings#MISSING_INDEX_ON_FOREIGN_KEY_CHILD)warning.

A foreign key constraint can be deferred until the transaction is complete. This is useful if you are doing bulk inserts into the database in a single transaction. By default, foreign key constraints are immediate but you can change this value by setting[deferred()](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#deferred())to`true`. You can also use[defer_foreign_keys](https://sqlite.org/pragma.html#pragma_defer_foreign_keys)PRAGMA to defer them depending on your transaction.

Please refer to the SQLite[foreign keys](https://sqlite.org/foreignkeys.html)documentation for details.

## Summary

|                                                                                                                                                                                          ### Nested classes                                                                                                                                                                                           ||
|---------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` @interface` | [ForeignKey.Action](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey.Action) Constants definition for values that can be used in[onDelete()](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#onDelete())and[onUpdate()](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#onUpdate()). |

|                                                                                                                                                                       ### Constants                                                                                                                                                                        ||
|-------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `int` | [CASCADE](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#CASCADE) Possible value for[onDelete()](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#onDelete())or[onUpdate()](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#onUpdate()).         |
| `int` | [NO_ACTION](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#NO_ACTION) Possible value for[onDelete()](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#onDelete())or[onUpdate()](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#onUpdate()).     |
| `int` | [RESTRICT](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#RESTRICT) Possible value for[onDelete()](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#onDelete())or[onUpdate()](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#onUpdate()).       |
| `int` | [SET_DEFAULT](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#SET_DEFAULT) Possible value for[onDelete()](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#onDelete())or[onUpdate()](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#onUpdate()). |
| `int` | [SET_NULL](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#SET_NULL) Possible value for[onDelete()](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#onDelete())or[onUpdate()](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#onUpdate()).       |

|                                                                                                                               ### Public methods                                                                                                                               ||
|-------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| ` String[]` | ` `[childColumns](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#childColumns())`() ` The list of column names in the current[Entity](https://developer.android.com/reference/android/arch/persistence/room/Entity).            |
| ` boolean`  | ` `[deferred](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#deferred())`() ` \* A foreign key constraint can be deferred until the transaction is complete.                                                                    |
| ` Class`    | ` `[entity](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#entity())`() ` The parent Entity to reference.                                                                                                                       |
| ` int`      | ` `[onDelete](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#onDelete())`() ` Action to take when the parent[Entity](https://developer.android.com/reference/android/arch/persistence/room/Entity)is deleted from the database. |
| ` int`      | ` `[onUpdate](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#onUpdate())`() ` Action to take when the parent[Entity](https://developer.android.com/reference/android/arch/persistence/room/Entity)is updated in the database.   |
| ` String[]` | ` `[parentColumns](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#parentColumns())`() ` The list of column names in the parent[Entity](https://developer.android.com/reference/android/arch/persistence/room/Entity).           |

| ### Inherited methods |
|-----------------------|---|
| From interface` java.lang.annotation.Annotation ` |-----------------------------------------|-------------------------| | ` abstract Class<? extends Annotation>` | ` annotationType() `    | | ` abstract boolean`                     | ` equals(Object arg0) ` | | ` abstract int`                         | ` hashCode() `          | | ` abstract String`                      | ` toString() `          | ||

## Constants

### CASCADE

```
int CASCADE
```

Possible value for[onDelete()](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#onDelete())or[onUpdate()](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#onUpdate()).

A "CASCADE" action propagates the delete or update operation on the parent key to each dependent child key. For[onDelete()](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#onDelete())action, this means that each row in the child entity that was associated with the deleted parent row is also deleted. For an[onUpdate()](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#onUpdate())action, it means that the values stored in each dependent child key are modified to match the new parent key values.

<br />

Constant Value: 5 (0x00000005)  

### NO_ACTION

```
int NO_ACTION
```

Possible value for[onDelete()](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#onDelete())or[onUpdate()](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#onUpdate()).

When a parent key is modified or deleted from the database, no special action is taken. This means that SQLite will not make any effort to fix the constraint failure, instead, reject the change.

<br />

Constant Value: 1 (0x00000001)  

### RESTRICT

```
int RESTRICT
```

Possible value for[onDelete()](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#onDelete())or[onUpdate()](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#onUpdate()).

The RESTRICT action means that the application is prohibited from deleting (for[onDelete()](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#onDelete())) or modifying (for[onUpdate()](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#onUpdate())) a parent key when there exists one or more child keys mapped to it. The difference between the effect of a RESTRICT action and normal foreign key constraint enforcement is that the RESTRICT action processing happens as soon as the field is updated - not at the end of the current statement as it would with an immediate constraint, or at the end of the current transaction as it would with a[deferred()](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#deferred())constraint.

Even if the foreign key constraint it is attached to is[deferred()](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#deferred()), configuring a RESTRICT action causes SQLite to return an error immediately if a parent key with dependent child keys is deleted or modified.

<br />

Constant Value: 2 (0x00000002)  

### SET_DEFAULT

```
int SET_DEFAULT
```

Possible value for[onDelete()](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#onDelete())or[onUpdate()](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#onUpdate()).

The "SET DEFAULT" actions are similar to[SET_NULL](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#SET_NULL), except that each of the child key columns is set to contain the columns default value instead of`NULL`.

<br />

Constant Value: 4 (0x00000004)  

### SET_NULL

```
int SET_NULL
```

Possible value for[onDelete()](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#onDelete())or[onUpdate()](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#onUpdate()).

If the configured action is "SET NULL", then when a parent key is deleted (for[onDelete()](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#onDelete())) or modified (for[onUpdate()](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#onUpdate())), the child key columns of all rows in the child table that mapped to the parent key are set to contain`NULL`values.

<br />

Constant Value: 3 (0x00000003)

## Public methods

### childColumns

```
String[] childColumns ()
```

The list of column names in the current[Entity](https://developer.android.com/reference/android/arch/persistence/room/Entity).

Number of columns must match the number of columns specified in[parentColumns()](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#parentColumns()).

<br />

|                              Returns                               ||
|------------|--------------------------------------------------------|
| `String[]` | The list of column names in the current Entity. <br /> |

### deferred

```
boolean deferred ()
```

\* A foreign key constraint can be deferred until the transaction is complete. This is useful if you are doing bulk inserts into the database in a single transaction. By default, foreign key constraints are immediate but you can change it by setting this field to`true`. You can also use[defer_foreign_keys](https://sqlite.org/pragma.html#pragma_defer_foreign_keys)PRAGMA to defer them depending on your transaction.

<br />

|                                                            Returns                                                             ||
|-----------|---------------------------------------------------------------------------------------------------------------------|
| `boolean` | Whether the foreign key constraint should be deferred until the transaction is complete. Defaults to`false`. <br /> |

### entity

```
Class entity ()
```

The parent Entity to reference. It must be a class annotated with[Entity](https://developer.android.com/reference/android/arch/persistence/room/Entity)and referenced in the same database.

<br />

|              Returns               ||
|---------|---------------------------|
| `Class` | The parent Entity. <br /> |

### onDelete

```
int onDelete ()
```

Action to take when the parent[Entity](https://developer.android.com/reference/android/arch/persistence/room/Entity)is deleted from the database.

By default,[NO_ACTION](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#NO_ACTION)is used.

<br />

|                                          Returns                                          ||
|-------|------------------------------------------------------------------------------------|
| `int` | The action to take when the referenced entity is deleted from the database. <br /> |

### onUpdate

```
int onUpdate ()
```

Action to take when the parent[Entity](https://developer.android.com/reference/android/arch/persistence/room/Entity)is updated in the database.

By default,[NO_ACTION](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#NO_ACTION)is used.

<br />

|                                         Returns                                         ||
|-------|----------------------------------------------------------------------------------|
| `int` | The action to take when the referenced entity is updated in the database. <br /> |

### parentColumns

```
String[] parentColumns ()
```

The list of column names in the parent[Entity](https://developer.android.com/reference/android/arch/persistence/room/Entity).

Number of columns must match the number of columns specified in[childColumns()](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#childColumns()).

<br />

|                              Returns                              ||
|------------|-------------------------------------------------------|
| `String[]` | The list of column names in the parent Entity. <br /> |

**See also:**

- [childColumns()](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey#childColumns())  
-

  ## Annotations

  - [ColumnInfo](https://developer.android.com/reference/android/arch/persistence/room/ColumnInfo)
  - [ColumnInfo.Collate](https://developer.android.com/reference/android/arch/persistence/room/ColumnInfo.Collate)
  - [ColumnInfo.SQLiteTypeAffinity](https://developer.android.com/reference/android/arch/persistence/room/ColumnInfo.SQLiteTypeAffinity)
  - [Dao](https://developer.android.com/reference/android/arch/persistence/room/Dao)
  - [Database](https://developer.android.com/reference/android/arch/persistence/room/Database)
  - [Delete](https://developer.android.com/reference/android/arch/persistence/room/Delete)
  - [Embedded](https://developer.android.com/reference/android/arch/persistence/room/Embedded)
  - [Entity](https://developer.android.com/reference/android/arch/persistence/room/Entity)
  - [ForeignKey](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey)
  - [ForeignKey.Action](https://developer.android.com/reference/android/arch/persistence/room/ForeignKey.Action)
  - [Ignore](https://developer.android.com/reference/android/arch/persistence/room/Ignore)
  - [Index](https://developer.android.com/reference/android/arch/persistence/room)
  - [Insert](https://developer.android.com/reference/android/arch/persistence/room/Insert)
  - [OnConflictStrategy](https://developer.android.com/reference/android/arch/persistence/room/OnConflictStrategy)
  - [PrimaryKey](https://developer.android.com/reference/android/arch/persistence/room/PrimaryKey)
  - [Query](https://developer.android.com/reference/android/arch/persistence/room/Query)
  - [RawQuery](https://developer.android.com/reference/android/arch/persistence/room/RawQuery)
  - [Relation](https://developer.android.com/reference/android/arch/persistence/room/Relation)
  - [SkipQueryVerification](https://developer.android.com/reference/android/arch/persistence/room/SkipQueryVerification)
  - [Transaction](https://developer.android.com/reference/android/arch/persistence/room/Transaction)
  - [TypeConverter](https://developer.android.com/reference/android/arch/persistence/room/TypeConverter)
  - [TypeConverters](https://developer.android.com/reference/android/arch/persistence/room/TypeConverters)
  - [Update](https://developer.android.com/reference/android/arch/persistence/room/Update)
-

  ## Classes

  - [DatabaseConfiguration](https://developer.android.com/reference/android/arch/persistence/room/DatabaseConfiguration)
  - [InvalidationTracker](https://developer.android.com/reference/android/arch/persistence/room/InvalidationTracker)
  - [InvalidationTracker.Observer](https://developer.android.com/reference/android/arch/persistence/room/InvalidationTracker.Observer)
  - [Room](https://developer.android.com/reference/android/arch/persistence/room/Room)
  - [RoomDatabase](https://developer.android.com/reference/android/arch/persistence/room/RoomDatabase)
  - [RoomDatabase.Builder](https://developer.android.com/reference/android/arch/persistence/room/RoomDatabase.Builder)
  - [RoomDatabase.Callback](https://developer.android.com/reference/android/arch/persistence/room/RoomDatabase.Callback)
  - [RoomDatabase.MigrationContainer](https://developer.android.com/reference/android/arch/persistence/room/RoomDatabase.MigrationContainer)
  - [RoomWarnings](https://developer.android.com/reference/android/arch/persistence/room/RoomWarnings)
  - [RxRoom](https://developer.android.com/reference/android/arch/persistence/room/RxRoom)
-

  ## Enums

  - [RoomDatabase.JournalMode](https://developer.android.com/reference/android/arch/persistence/room/RoomDatabase.JournalMode)
-

  ## Exceptions

  - [EmptyResultSetException](https://developer.android.com/reference/android/arch/persistence/room/EmptyResultSetException)