房间返回很长但没有将数据插入表中-一对多关系

时间:2019-05-27 05:05:07

标签: android kotlin android-room

我有两个实体类

@Entity(tableName = "TrackingItemTable")
data class TrackingItem(
                        @ColumnInfo val ProductName:String,
                        @ColumnInfo val ProductPrice:Float,
                        @ColumnInfo val ProductImage:String,
                        @ColumnInfo val ProductCurrencySymbol:String,
                        @ColumnInfo val ProductAddedDate: Date,
                        @ColumnInfo val ProductSiteName:String,
                        @PrimaryKey @ColumnInfo val ProductUrl:String,
                        @ColumnInfo val ProductAffiliateUrl:String,
                        @ColumnInfo var ProductNotificationPrice:Float? = null,
                        @ColumnInfo var ProductLastUpdatedPrice:Float
)

,并且与其他实体具有预定的密钥关系

@Entity(foreignKeys = arrayOf(ForeignKey(entity = TrackingItem::class,
    parentColumns = arrayOf("ProductUrl"),
    childColumns = arrayOf("ProductAnalyticsUrl"),
    onDelete = ForeignKey.CASCADE)), tableName = "TrackingItemDataTable")



data class TrackingItemData(@ColumnInfo val ProductAnalyticsUrl:String,
                            @ColumnInfo val Price:Float,
                            @ColumnInfo val Date: Long,
                            @PrimaryKey(autoGenerate = true) @ColumnInfo var id:Int = 0
)

每当我尝试在TrackingItemData表中插入一行时,都没有插入该行,但是房间返回Long值,这是我的目标

@Dao
interface TrackingItemDataDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertTrackingItemData(trackingItemData:TrackingItemData): Single<Long>

    @Query("SELECT * FROM TrackingItemDataTable")
    fun getAllTrackingItems():LiveData<List<TrackingItemData>>

}

我插入这样的数据

Db.analyticsDao().insertTrackingItemData(
    TrackingItemData(ProductAnalyticsUrl = url,
        Price = currentPrice,
        Date = currentTime))
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe({
        Log.d("shopzy", it.toString())
    },{
        Log.d("shopzy", it.toString())
    })

rx java的throwable没有捕获任何异常,代码显示了long值,但是当我从模拟器下载db并查看表时,我没有在其中找到一行,而dao的getAllTrackingItems()返回0行,我做错了什么?

1 个答案:

答案 0 :(得分:0)

出于某些奇怪的原因,从第二个实体类中删除tableName =“ TrackingItemDataTable”解决了该问题,不确定为什么会这样做