SK1dev
SK1dev

Reputation: 1139

Room: Conflicting Declarations

I would like to add value, date and details to the current pb. I am receiving an error 'conflicting declaration' in the database for pbInfo. How should I fix this error?

@Entity(tableName = "pb_table")
data class Pb(@PrimaryKey
              val pb: String)

@Entity
data class PbInfo(@PrimaryKey
                  var value: Double,
                  var date: Int,
                  var details: String)

@Dao
interface PbInfoDao {


    @Insert
    fun update(vararg pbInfo: PbInfo): LongArray

 INSTANCE?.let { database ->
                    scope.launch {
                        populateDatabase(database.pbDao(), database.pbInfo())
                    }
                }
            }
            suspend fun populateDatabase(pbDao: PbDao, pbInfoDao: PbInfoDao) {
                pbDao.deleteAll()

                var pb = Pb("Squat")
                pbDao.insert(pb)
                var pbInfo = PbInfo(122.5, 28, "I was feeling on top form today!")

Upvotes: 1

Views: 1776

Answers (2)

Farhan Ibn Wahid
Farhan Ibn Wahid

Reputation: 1012

First of all, you have two Entities in a single class (possibly the conflict)

So, add separate class for separate Entity.

Then, in your RoomDatabase abstract class, add two Entity Classes like this (and also create separate Dao interface classes):

@Database(entities = [(Pb::class), (Pbinfo::class)] ,version = 2)
abstract class YourRoomDatabaseClass: RoomDatabase(){
    ...
     abstract fun pbDao() : PbDao
     abstract fun pbinfoDao(): PbinfoDao
    ...
}

This should solve the conflicting of Entity classes. I have a single database with two Entities just like this and running without any problems. (Please mind me because I don't know Kotlin Syntax)

Upvotes: 1

Rajnish suryavanshi
Rajnish suryavanshi

Reputation: 3414

Use this

@Insert(onConflict = OnConflictStrategy.REPLACE)

instead of

@Insert

Upvotes: 0

Related Questions