title: Room - How to set nullable Foreign Key
body: <p>In my Kotlin Android codebase I have the following 2 entities..</p>
  <pre><code>@Entity(
  tableName = ModuleConfiguration.tableName,
  primaryKeys = [ModuleConfiguration.COL_ID],
  foreignKeys = arrayOf(
      ForeignKey(
          entity = Module::class,
          parentColumns = [Module.COL_ID],
          childColumns = [ModuleConfiguration.COL_MODULE_ID],
          onDelete = ForeignKey.CASCADE
      ),
      ForeignKey(
          entity = Group::class,
          parentColumns = [Group.COL_ID],
          childColumns = [ModuleConfiguration.COL_GROUP_ID]
      )
  )
  )
  class ModuleConfiguration(
  @ColumnInfo(name = COL_ID)
  var id: String = UUID.randomUUID().toString(),
  
  @ColumnInfo(name = COL_TABLE)
  var table: String,
  
  @ColumnInfo(name = COL_FIELD_NAME)
  var fieldName: String,
  
  @ColumnInfo(name = COL_FIELD_LABEL)
  var fieldLabel: String,
  
  @ColumnInfo(name = COL_FIELD_TYPE)
  var fieldType: ModuleConfigurationItemType,
  
  @ColumnInfo(name = COL_GROUP_ID)
  var groupId: String?,
  
  @ColumnInfo(name = COL_MODULE_ID)
  var moduleId: String,
  
  @ColumnInfo(name = COL_POSITION)
  var position: Int,
  
  @ColumnInfo(name = COL_VISIBLE)
  var visible: Int = 1, //Usually visible
  
  @ColumnInfo(name = COL_READ_ONLY)
  var readOnly: Int = 0, //Usually false
  
  @ColumnInfo(name = COL_REQUIRED)
  var required: Int = 0, //Usually false
  
  @ColumnInfo(name = COL_CREATED_AT)
  var createdAt: Long = CustomDateTimeUtil.getTodayInUTC(),
  
  @ColumnInfo(name = COL_UPDATED_AT)
  var updatedAt: Long = CustomDateTimeUtil.getTodayInUTC()
  ) : Cloneable, Serializable, Parcelable  {
  
  constructor(parcel: Parcel) : this(
      parcel.readString() ?: &quot;&quot;,
      parcel.readString() ?: &quot;&quot;,
      parcel.readString() ?: &quot;&quot;,
      parcel.readString() ?: &quot;&quot;,
      fieldType = ModuleConfigurationItemType.valueOf(parcel.readString() ?: FieldType.UNKNOWN.name),
      groupId = parcel.readString(),
      moduleId = parcel.readString() ?: &quot;&quot;,
      position = parcel.readInt(),
      visible = parcel.readInt(),
      readOnly = parcel.readInt(),
      required = parcel.readInt(),
      createdAt = parcel.readLong(),
      updatedAt = parcel.readLong()
  ) {
  }
  
  fun getViewType() : ModuleConfigurationItemType {
      return this.fieldType
  }
  
  override fun equals(other: Any?): Boolean {
      return super.equals(other)
  }
  
  override fun hashCode(): Int {
      return super.hashCode()
  }
  
  override fun clone(): Any {
      return super.clone()
  }
  
  override fun writeToParcel(parcel: Parcel, flags: Int) {
      parcel.writeString(id)
      parcel.writeString(table)
      parcel.writeString(fieldName)
      parcel.writeString(fieldLabel)
      parcel.writeString(fieldType.name)
      parcel.writeString(groupId)
      parcel.writeString(moduleId)
      parcel.writeInt(position)
      parcel.writeInt(visible)
      parcel.writeInt(readOnly)
      parcel.writeInt(required)
      parcel.writeLong(createdAt)
      parcel.writeLong(updatedAt)
  }
  
  override fun describeContents(): Int {
      return 0
  }
  
  companion object CREATOR : Parcelable.Creator&lt;ModuleConfiguration&gt; {
  
      const val tableName = &quot;module_configuration&quot;
  
      const val COL_ID = &quot;id&quot;
      const val COL_MODULE_ID = &quot;module_id&quot;
      const val COL_TABLE = &quot;table&quot;
      const val COL_FIELD_NAME = &quot;field_name&quot;
      const val COL_FIELD_LABEL = &quot;field_label&quot;
      const val COL_FIELD_TYPE = &quot;field_type&quot;
      const val COL_GROUP_ID = &quot;group_id&quot;
      const val COL_VISIBLE = &quot;visible&quot;
      const val COL_READ_ONLY = &quot;read_only&quot;
      const val COL_REQUIRED = &quot;required&quot;
      const val COL_POSITION = &quot;position&quot;
      const val COL_CREATED_AT = &quot;created_at&quot;
      const val COL_UPDATED_AT = &quot;updated_at&quot;
      const val COL_CLIENT_ID = &quot;client_id&quot;
  
      override fun createFromParcel(parcel: Parcel): ModuleConfiguration {
          return ModuleConfiguration(parcel)
      }
  
      override fun newArray(size: Int): Array&lt;ModuleConfiguration?&gt; {
          return arrayOfNulls(size)
      }
  }
  }
  </code></pre>
  <p>and Group Entity</p>
  <pre><code>@Entity(
  tableName = Group.tableName,
  primaryKeys = [Group.COL_ID]
  )
  class Group(
  @ColumnInfo(name = COL_ID)
  var id: String = UUID.randomUUID().toString(),
  
  @ColumnInfo(name = COL_NAME)
  var name: String,
  
  @ColumnInfo(name = COL_CREATED_AT)
  var createdAt: Long = CustomDateTimeUtil.getTodayInUTC(),
  
  @ColumnInfo(name = COL_UPDATED_AT)
  var updatedAt: Long = CustomDateTimeUtil.getTodayInUTC()
  ) : Cloneable, Parcelable  {
  
  constructor(parcel: Parcel) : this(
      parcel.readString() ?: &quot;&quot;,
      parcel.readString() ?: &quot;&quot;,
      parcel.readLong(),
      parcel.readLong()
  ) {
  }
  
  override fun equals(other: Any?): Boolean {
      return super.equals(other)
  }
  
  override fun hashCode(): Int {
      return super.hashCode()
  }
  
  override fun clone(): Any {
      return super.clone()
  }
  
  override fun writeToParcel(parcel: Parcel, flags: Int) {
      parcel.writeString(id)
      parcel.writeString(name)
      parcel.writeLong(createdAt)
      parcel.writeLong(updatedAt)
  }
  
  override fun describeContents(): Int {
      return 0
  }
  
  companion object CREATOR : Parcelable.Creator&lt;Group&gt; {
  
      const val tableName = &quot;group&quot;
  
      const val COL_ID = &quot;id&quot;
      const val COL_NAME = &quot;name&quot;
      const val COL_CREATED_AT = &quot;created_at&quot;
      const val COL_UPDATED_AT = &quot;updated_at&quot;
      const val COL_CLIENT_ID = &quot;client_id&quot;
  
      override fun createFromParcel(parcel: Parcel): Group {
          return Group(parcel)
      }
  
      override fun newArray(size: Int): Array&lt;Group?&gt; {
          return arrayOfNulls(size)
      }
  }
  }
  </code></pre>
  <p>My problem is that I am trying to set nullable foreign key, as you can see in entity named ModuleConfiguration, there is column named group_id, which is nullable.</p>
  <pre><code>@ColumnInfo(name = COL_GROUP_ID)
  var groupId: String?,
  </code></pre>
  <p>Since, this column belongs to other entity named Group, I am trying to make it foreign key, but when I do that, I am getting following error</p>
  <p>SQLiteConstraintException: FOREIGN KEY constraint failed (code 787)</p>
  <p>On searching online, I found following answers on stackoverflow:</p>
  <p><a href="https://stackoverflow.com/questions/48933887/android-with-room-how-to-set-a-foreign-key-nullable">Android with Room - How to set a foreign key nullable</a>
  or
  <a href="https://stackoverflow.com/questions/56492719/nullable-foreign-key-on-room">Nullable foreign key on room</a></p>
  <p>but this did not help me as these answer suggest to change primitive types like int or long into non-primitive types like Integer or Long for allowing them to be null. I am already using String as type.</p>
  <p>any help would be highly appreciated.</p>
  <p>Thanks in advance.</p>