Kotlin 房间-如何设置可为空的外键
在我的Kotlin Android代码库中,我有以下两个实体Kotlin 房间-如何设置可为空的外键,kotlin,foreign-keys,android-room,room,Kotlin,Foreign Keys,Android Room,Room,在我的Kotlin Android代码库中,我有以下两个实体 @Entity( tableName = ModuleConfiguration.tableName, primaryKeys = [ModuleConfiguration.COL_ID], foreignKeys = arrayOf( ForeignKey( entity = Module::class, parentColumns = [Module.COL_ID], chi
@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() ?: "",
parcel.readString() ?: "",
parcel.readString() ?: "",
parcel.readString() ?: "",
fieldType = ModuleConfigurationItemType.valueOf(parcel.readString() ?: FieldType.UNKNOWN.name),
groupId = parcel.readString(),
moduleId = parcel.readString() ?: "",
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<ModuleConfiguration> {
const val tableName = "module_configuration"
const val COL_ID = "id"
const val COL_MODULE_ID = "module_id"
const val COL_TABLE = "table"
const val COL_FIELD_NAME = "field_name"
const val COL_FIELD_LABEL = "field_label"
const val COL_FIELD_TYPE = "field_type"
const val COL_GROUP_ID = "group_id"
const val COL_VISIBLE = "visible"
const val COL_READ_ONLY = "read_only"
const val COL_REQUIRED = "required"
const val COL_POSITION = "position"
const val COL_CREATED_AT = "created_at"
const val COL_UPDATED_AT = "updated_at"
const val COL_CLIENT_ID = "client_id"
override fun createFromParcel(parcel: Parcel): ModuleConfiguration {
return ModuleConfiguration(parcel)
}
override fun newArray(size: Int): Array<ModuleConfiguration?> {
return arrayOfNulls(size)
}
}
}
由于该列属于另一个名为Group的实体,所以我试图将其设为外键,但当我这样做时,会出现以下错误
SQLiteConstraintException:外键约束失败(代码787)
在网上搜索时,我在stackoverflow上找到了以下答案:
或
但这并没有帮助我,因为这些答案建议将int或long等基本类型更改为Integer或long等非基本类型,以允许它们为null。我已经在使用字符串作为类型
任何帮助都将不胜感激
提前谢谢。请尝试重新表述这个问题,因为它确实很难理解,这就是为什么你会被否决@被否决的人:如果你留下评论,那就太好了。好的,现在就做:(现在好点了吗?是不是另一个外键
moduleId
的约束问题不可为空?也许你是对的,一旦我删除了两个外键约束,它就工作了..删除我认为是导致问题的外键也没能解决..经过一些尝试..我想这可能是顺序问题。。我是如何添加值的…因为有相当多的行是我启动数据库时使用的..现在检查它们
@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() ?: "",
parcel.readString() ?: "",
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<Group> {
const val tableName = "group"
const val COL_ID = "id"
const val COL_NAME = "name"
const val COL_CREATED_AT = "created_at"
const val COL_UPDATED_AT = "updated_at"
const val COL_CLIENT_ID = "client_id"
override fun createFromParcel(parcel: Parcel): Group {
return Group(parcel)
}
override fun newArray(size: Int): Array<Group?> {
return arrayOfNulls(size)
}
}
}
@ColumnInfo(name = COL_GROUP_ID)
var groupId: String?,