Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如果挂起的@Insert具有相同的字段值,则替换数据库行_Android_Kotlin_Android Room - Fatal编程技术网

Android 如果挂起的@Insert具有相同的字段值,则替换数据库行

Android 如果挂起的@Insert具有相同的字段值,则替换数据库行,android,kotlin,android-room,Android,Kotlin,Android Room,My Room方法addMatchUid()不符合其冲突策略。请替换。它只添加相同的数据库行: 这是我的数据类: MatchedUser.kt @Entity(tableName = "matched_users") data class MatchedUser( @PrimaryKey(autoGenerate = true) val id: Int, @ColumnInfo(name = "match_id") val matchId: String ) @Dao int

My Room方法
addMatchUid()
不符合其冲突策略。请替换
。它只添加相同的数据库行:

这是我的数据类:

MatchedUser.kt

@Entity(tableName = "matched_users")
data class MatchedUser(
    @PrimaryKey(autoGenerate = true) val id: Int,
    @ColumnInfo(name = "match_id") val matchId: String
)
@Dao
interface MatchedUsersDao {

    @Query("SELECT COUNT(*) FROM matched_users WHERE :matchId = match_id LIMIT 1")
    suspend fun matchedBefore(matchId: String): Int

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun addMatchUid(match: MatchedUser)
}
MatchedUsersDao.kt

@Entity(tableName = "matched_users")
data class MatchedUser(
    @PrimaryKey(autoGenerate = true) val id: Int,
    @ColumnInfo(name = "match_id") val matchId: String
)
@Dao
interface MatchedUsersDao {

    @Query("SELECT COUNT(*) FROM matched_users WHERE :matchId = match_id LIMIT 1")
    suspend fun matchedBefore(matchId: String): Int

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun addMatchUid(match: MatchedUser)
}
下面是正在执行的
addMatchId()

val match = MatchedUser(0, matchId)
CustomApplication.database?.matchedUsersDao()?.addMatchUid(match)

我对这个问题的唯一猜测是,数据库行实际上不会重复,因为id不同。如果是这种情况,当
match\u id
的值相同时,如何确保行被替换?

您可以在
match\u id
上添加
unique
索引以获得所需的行为

@Entity(tableName = "matched_users", indices = [Index(value = ["match_id"], unique = true)])
data class MatchedUser(
    @PrimaryKey(autoGenerate = true) val id: Int,
    @ColumnInfo(name = "match_id") val matchId: String
)