Issue
I'm new to Room and haven't find any posts related to this. I have two classes: BaseModel and ChildModel. ChildModel is a Room entity class, but BaseModel - isn't. How do I extend BaseModel from ChildModel properly? I tried to do it as in the code bellow...
BaseModel.kt:
@Parcelize
open class BaseModel(
open val id: String,
open var name: String,
open var creator: String,
open var dateOfLastEdit: Timestamp,
open var contributors: List<String>,
open var color: Int,
open var offline: Boolean
) : Parcelable {...}
ChildModel.kt (empty constructor used for firebase):
@Entity
@Parcelize
data class ChildModel(
@PrimaryKey(autoGenerate = false)
override val id: String = "",
override var name: String = "",
override var creator: String = "",
@TypeConverters(DateTypeConverter::class)
override var dateOfLastEdit: Timestamp = currentTime(),
@TypeConverters(ListStringConverter::class)
override var contributors: List<String> = emptyList(),
override var color: Int = NO_COLOR,
override var offline: Boolean = false,
var text: String = "",
var lastEditTextPosition: Int = 0,
var lastEditNamePosition: Int = 0,
var lastUserEdited: String = ""
) : Parcelable, BaseModel(id, name, creator, dateOfLastEdit, contributors, color, offline) {
constructor() : this("", "", "", currentTime(), emptyList(), NO_COLOR, false, "", 0, 0, "")
...
}
... but it gives error: "Field has non-unique column name" for all the fields which are overridden from BaseModel. Btw, when create BaseModel as an interface it builds successfully.
My database class:
@Database(entities = [ChildModel::class], version = 1)
@TypeConverters(DateTypeConverter::class, ListStringConverter::class)
abstract class CustomDatabase : RoomDatabase() {
abstract fun childModelDao(): ChildModelDao
companion object {
var INSTANCE: CustomDatabase? = null
fun getDatabase(context: Context): CustomDatabase? {
if (INSTANCE == null) {
synchronized(CustomDatabase::class) {
INSTANCE = Room.databaseBuilder(
context.applicationContext,
CustomDatabse::class.java,
DATABASE
).build()
}
}
return INSTANCE
}
fun destroyDatabase() {
INSTANCE = null
}
}
}
The error is not about upgrading database version, because I deleted the previous one.
Thanks in advance!
Solution
Ok, it seems that Room somehow creates two columns with same names (for parent class and for child class) I tried adding @Ignore
annotation above every field inside BaseModel class. It fixed the issue, the project builds successfully, but I'm not sure that's the best approach.
Answered By - Anastasia Dronina
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.