简体   繁体   English

使用房间时发生 Parcelable 错误

[英]Parcelable error occurred while using the Room

Parcelable error occurred while using the Room.使用房间时发生 Parcelable 错误。

I'm studying Room, but I don't have much common sense yet.我正在学习Room,但我还没有太多常识。 So I don't know why I use Parcelable.所以我不知道为什么我使用 Parcelable。

I think there was an error when changing the format of Room to one to many.我认为将 Room 的格式更改为一对多时出现错误。

error mesage:错误信息:

error: (goalId) does not exist in com.example.goalapp.db.entity.Goal. Available columns are id,goalName

public final class TodayGoal implements android.os.Parcelable {
                                                              ^

Entity related code:实体相关代码:

//Goal table
@Parcelize
@Entity(tableName = "goal_table")
data class Goal(
    @PrimaryKey(autoGenerate = true)
    val id: Int,
    val goalName: String,
): Parcelable


//todayGoal table
@Parcelize
@Entity(
    tableName = "today_goal_table",
    foreignKeys = [
        ForeignKey(
            entity = Goal::class,
            parentColumns = ["goalId"],
            childColumns = ["id"],
            onDelete = ForeignKey.CASCADE
        )
    ]
)
data class TodayGoal(
    val id: Int,
    @PrimaryKey(autoGenerate = true)
    val todayGoalId: Int,
    val TodayGoalName: String,
): Parcelable


//GoalAndTodayGoal
@Parcelize
data class GoalAndTodayGoals(
    @Embedded val goal: Goal,
    @Relation(
        parentColumn = "goalId",
        entityColumn = "id"
    )
    val todayGoals: List<TodayGoal>
): Parcelable

As the error say, goalId does not exist in Goal class.正如错误所说, Goal class中不存在goalId Try to change the id attribute of Goal to goalId .尝试将Goalid属性更改为goalId

@Parcelize
@Entity(tableName = "goal_table")
data class Goal(
    @PrimaryKey(autoGenerate = true)
    val goalId: Int,
    val goalName: String,
): Parcelable

The Issue问题

Your issue is that the reference, in the ForeignKey definition as per parentColumns = ["goalId"], , is not pointing to a column that exists in the parent ( Goal ) table.您的问题是,根据parentColumns = ["goalId"],在 ForeignKey 定义中的引用未指向父( Goal )表中存在的列。

The respective (suitable) column is named id as per @PrimaryKey(autoGenerate = true) val id: Int,相应的(合适的)列根据@PrimaryKey(autoGenerate = true) val id: Int,命名为id

  • if you used the goalName column that Room tells you is an available column then you would get a subsequent error as the goalName column is not UNIQUE.如果您使用Room告诉您的目标名称列是可用列,那么您将收到后续错误,因为目标名称列不是唯一的。

    • a ForeignKey must reference a parent column (or combination of columns) that is/are UNIQUE , as SQLite must be able to ascertain the exact parent not one of more than one parent. ForeignKey必须引用UNIQUE的父列(或列组合),因为 SQLite 必须能够确定确切的父列,而不是一个以上的父列。

Potential Fixes潜在的修复

1. change the id column name to be goalId so either:- 1.id列名称更改为goalId ,因此:-

val goalId: Int,

or if you want the field/member name to remain as id then:-或者,如果您希望字段/成员名称保留为id ,则:-

@ColumnInfo(name = "goalId")
val id: Int
  • Note one of these 2 solutions would be suggested as the better solutions.请注意,建议将这两种解决方案之一作为更好的解决方案。

2. Change, to use parentColumns = ["id"], . 2.更改为使用parentColumns = ["id"],

  • However , using id as column, if not the only column named id, in related tables, can lead to ambiguities (what id column).但是,使用id作为列,如果不是相关表中唯一名为 id 的列,可能会导致歧义(什么 id 列)。

    • this issue would not be encountered if not using JOIN in a SELECT statement.如果不在SELECT语句中使用JOIN ,则不会遇到此问题。
    • The ambiguity and can be circumvented, by using JOIN along with using exact column names, of the ambiguous columns prefixed by the table name and separated from the column name by a .可以通过使用JOIN以及使用确切的列名来规避以表名为前缀并由. , but that can lead to rather lengthy SQL. ,但这可能会导致相当长的 SQL。

3. Another option would be to do away with the ForeignKey definition altogether BUT even though it isn't needed 3.另一种选择是完全取消ForeignKey定义,即使它不是必需的

  • ForeignKey does not define a relationship, but instead creates a rule (a constraint in SQLite terminology) that enforces referential integrity ForeignKey 不定义关系,而是创建一个强制参照完整性的规则(SQLite 术语中的约束

  • However, you then lose the built-in assurance of referential integrity and the automatic maintenance of the referential integrity (deletion of a parent being CASCADE ed (ie the children being deleted if the parent is deleted)).但是,您将失去参照完整性的内置保证和参照完整性的自动维护(删除父级被CASCADE ed(即,如果删除父级,则删除子级))。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM