# ForeignKey

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

*** ** * ** ***

Common/AllAndroid/JVMNative/C/iOSJavaScript
- Cmn

  ```
  @Target(allowedTargets = )
  @Retention(value = AnnotationRetention.BINARY)
  annotation ForeignKey
  ```

*** ** * ** ***

Declares a foreign key on another `https://developer.android.com/reference/kotlin/androidx/room3/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 output 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 output the `https://developer.android.com/reference/kotlin/androidx/room3/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 `https://developer.android.com/reference/kotlin/androidx/room3/ForeignKey#deferred()` to `true`. You can also use [defer_foreign_keys](https://sqlite.org/pragma.html#pragma_defer_foreign_keys) to defer them depending on your transaction.

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

## Summary

| ### Nested types |
|---|
| `@https://developer.android.com/reference/kotlin/androidx/annotation/IntDef(value = [1, 2, 3, 4, 5]) @https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.annotation/-retention/index.html(value = https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.annotation/-annotation-retention/-b-i-n-a-r-y/index.html) annotation https://developer.android.com/reference/kotlin/androidx/room3/ForeignKey.Action` Constants definition for values that can be used in `https://developer.android.com/reference/kotlin/androidx/room3/ForeignKey#onDelete()` and `https://developer.android.com/reference/kotlin/androidx/room3/ForeignKey#onUpdate()`. |

| ### Constants |
|---|---|---|
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/room3/ForeignKey#CASCADE() = 5` Possible value for `https://developer.android.com/reference/kotlin/androidx/room3/ForeignKey#onDelete()` or `https://developer.android.com/reference/kotlin/androidx/room3/ForeignKey#onUpdate()`. | Cmn |
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/room3/ForeignKey#NO_ACTION() = 1` Possible value for `https://developer.android.com/reference/kotlin/androidx/room3/ForeignKey#onDelete()` or `https://developer.android.com/reference/kotlin/androidx/room3/ForeignKey#onUpdate()`. | Cmn |
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/room3/ForeignKey#RESTRICT() = 2` Possible value for `https://developer.android.com/reference/kotlin/androidx/room3/ForeignKey#onDelete()` or `https://developer.android.com/reference/kotlin/androidx/room3/ForeignKey#onUpdate()`. | Cmn |
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/room3/ForeignKey#SET_DEFAULT() = 4` Possible value for `https://developer.android.com/reference/kotlin/androidx/room3/ForeignKey#onDelete()` or `https://developer.android.com/reference/kotlin/androidx/room3/ForeignKey#onUpdate()`. | Cmn |
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/room3/ForeignKey#SET_NULL() = 3` Possible value for `https://developer.android.com/reference/kotlin/androidx/room3/ForeignKey#onDelete()` or `https://developer.android.com/reference/kotlin/androidx/room3/ForeignKey#onUpdate()`. | Cmn |

| ### Public constructors |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/room3/ForeignKey#ForeignKey(kotlin.reflect.KClass,kotlin.Array,kotlin.Array,kotlin.Int,kotlin.Int,kotlin.Boolean)( entity: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.reflect/-k-class/index.html<*>, parentColumns: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html>, childColumns: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html>, onDelete: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, onUpdate: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, deferred: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html )` | Cmn |

| ### Public properties |
|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html>` | `https://developer.android.com/reference/kotlin/androidx/room3/ForeignKey#childColumns()` The list of column names in the current `https://developer.android.com/reference/kotlin/androidx/room3/Entity`. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/room3/ForeignKey#deferred()` A foreign key constraint can be deferred until the transaction is complete. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.reflect/-k-class/index.html<*>` | `https://developer.android.com/reference/kotlin/androidx/room3/ForeignKey#entity()` The parent Entity to reference. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/room3/ForeignKey#onDelete()` Action to take when the parent `https://developer.android.com/reference/kotlin/androidx/room3/Entity` is deleted from the database. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/room3/ForeignKey#onUpdate()` Action to take when the parent `https://developer.android.com/reference/kotlin/androidx/room3/Entity` is updated in the database. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html>` | `https://developer.android.com/reference/kotlin/androidx/room3/ForeignKey#parentColumns()` The list of column names in the parent `https://developer.android.com/reference/kotlin/androidx/room3/Entity`. | Cmn |

## Constants

### CASCADE

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

```
const val CASCADE = 5: Int
```

Possible value for `https://developer.android.com/reference/kotlin/androidx/room3/ForeignKey#onDelete()` or `https://developer.android.com/reference/kotlin/androidx/room3/ForeignKey#onUpdate()`.

A `CASCADE` action propagates the delete or update operation on the parent key to each dependent child key. For `https://developer.android.com/reference/kotlin/androidx/room3/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 `https://developer.android.com/reference/kotlin/androidx/room3/ForeignKey#onUpdate()` action, it means that the values stored in each dependent child key are modified to match the new parent key values.

### NO_ACTION

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

```
const val NO_ACTION = 1: Int
```

Possible value for `https://developer.android.com/reference/kotlin/androidx/room3/ForeignKey#onDelete()` or `https://developer.android.com/reference/kotlin/androidx/room3/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.

### RESTRICT

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

```
const val RESTRICT = 2: Int
```

Possible value for `https://developer.android.com/reference/kotlin/androidx/room3/ForeignKey#onDelete()` or `https://developer.android.com/reference/kotlin/androidx/room3/ForeignKey#onUpdate()`.

The `RESTRICT` action means that the application is prohibited from deleting (for `https://developer.android.com/reference/kotlin/androidx/room3/ForeignKey#onDelete()`) or modifying (for `https://developer.android.com/reference/kotlin/androidx/room3/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 property 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 `https://developer.android.com/reference/kotlin/androidx/room3/ForeignKey#deferred()` constraint.

Even if the foreign key constraint is attached to is `https://developer.android.com/reference/kotlin/androidx/room3/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.

### SET_DEFAULT

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

```
const val SET_DEFAULT = 4: Int
```

Possible value for `https://developer.android.com/reference/kotlin/androidx/room3/ForeignKey#onDelete()` or `https://developer.android.com/reference/kotlin/androidx/room3/ForeignKey#onUpdate()`.

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

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/room3/ColumnInfo#defaultValue()` |   |

### SET_NULL

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

```
const val SET_NULL = 3: Int
```

Possible value for `https://developer.android.com/reference/kotlin/androidx/room3/ForeignKey#onDelete()` or `https://developer.android.com/reference/kotlin/androidx/room3/ForeignKey#onUpdate()`.

If the configured action is `SET NULL`, then when a parent key is deleted (for `https://developer.android.com/reference/kotlin/androidx/room3/ForeignKey#onDelete()`) or modified (for `https://developer.android.com/reference/kotlin/androidx/room3/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.

## Public constructors

### ForeignKey

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

```
ForeignKey(
    entity: KClass<*>,
    parentColumns: Array<String>,
    childColumns: Array<String>,
    onDelete: Int = NO_ACTION,
    onUpdate: Int = NO_ACTION,
    deferred: Boolean = false
)
```

## Public properties

### childColumns

Cmn

```
val childColumns: Array<String>
```

The list of column names in the current `https://developer.android.com/reference/kotlin/androidx/room3/Entity`.

Number of columns must match the number of columns specified in `https://developer.android.com/reference/kotlin/androidx/room3/ForeignKey#(kotlin.reflect.KClass,kotlin.Array,kotlin.Array,kotlin.Int,kotlin.Int,kotlin.Boolean)`.

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html>` | The list of column names in the current Entity. |

### deferred

Cmn

```
val deferred: Boolean
```

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

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | Whether the foreign key constraint should be deferred until the transaction is complete. Defaults to `false`. |

### entity

Cmn

```
val entity: KClass<*>
```

The parent Entity to reference. It must be a class annotated with `https://developer.android.com/reference/kotlin/androidx/room3/Entity` and referenced in the same database.

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.reflect/-k-class/index.html<*>` | The parent Entity. |

### onDelete

Cmn

```
val onDelete: Int
```

Action to take when the parent `https://developer.android.com/reference/kotlin/androidx/room3/Entity` is deleted from the database.

By default, `https://developer.android.com/reference/kotlin/androidx/room3/ForeignKey#NO_ACTION()` is used.

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The action to take when the referenced entity is deleted from the database. |

### onUpdate

Cmn

```
val onUpdate: Int
```

Action to take when the parent `https://developer.android.com/reference/kotlin/androidx/room3/Entity` is updated in the database.

By default, `https://developer.android.com/reference/kotlin/androidx/room3/ForeignKey#NO_ACTION()` is used.

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The action to take when the referenced entity is updated in the database. |

### parentColumns

Cmn

```
val parentColumns: Array<String>
```

The list of column names in the parent `https://developer.android.com/reference/kotlin/androidx/room3/Entity`.

Number of columns must match the number of columns specified in `https://developer.android.com/reference/kotlin/androidx/room3/ForeignKey#(kotlin.reflect.KClass,kotlin.Array,kotlin.Array,kotlin.Int,kotlin.Int,kotlin.Boolean)`.

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html>` | The list of column names in the parent Entity. |

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/room3/ForeignKey#(kotlin.reflect.KClass,kotlin.Array,kotlin.Array,kotlin.Int,kotlin.Int,kotlin.Boolean)` |   |