I have a requirement for two different Room
tables with the same ShoppingListItem Entity
. So, I recreated another Entity
data class model with the same properties but with a different name, CustomSortShoppingListItemEntity
. But, Room throws the error stating that there is no such table which is specified as custom_sort_shopping_list_items
in the CustomSortShoppingListItemEntity
data class. Do I have to create a separate Dao
for the custom_sort_shopping_list_items
table? How can I fix this?
Error message
error: There is a problem with the query: [SQLITE_ERROR] SQL error or missing database (no such table: custom_sort_shopping_list_items)
public abstract java.lang.Object countCustomSortListItems(long listId, @org.jetbrains.annotations.NotNull()
ShoppingListItemEntity
@Entity(
tableName = "shopping_list_items",
foreignKeys = [ForeignKey(
entity = ShoppingListEntity::class,
parentColumns = arrayOf("id"),
childColumns = arrayOf("shopping_list_id"),
onUpdate = ForeignKey.CASCADE,
onDelete = ForeignKey.CASCADE
)]
)
data class ShoppingListItemEntity(
@PrimaryKey(autoGenerate = true)
var id: Long = 0L,
@ColumnInfo(name = "shopping_list_id")
var shoppingListId: Long = 0L,
@ColumnInfo(name = "name")
val name: String,
@ColumnInfo(name = "category")
val category: String,
@ColumnInfo(name = "quantity")
val quantity: String,
@ColumnInfo(name = "set_quantity")
val setQuantity: String,
@ColumnInfo(name = "set_total")
val setTotal: String,
@ColumnInfo(name = "unit")
val unit: String,
@ColumnInfo(name = "item_ppu")
val itemPPU: String,
@ColumnInfo(name = "item_set_ppu")
val itemSetPPU: String,
@ColumnInfo(name = "notes")
val notes: String,
@ColumnInfo(name = "coupon_amount")
val couponAmount: String,
@ColumnInfo(name = "set_coupon_amount")
val setCouponAmount: String,
@ColumnInfo(name = "item_total")
val itemTotal: String,
@ColumnInfo(name = "item_set_total")
val itemSetTotal: String,
@ColumnInfo(name = "image_uri")
val itemImageUri: String?,
@ColumnInfo(name = "thumbnail_uri")
val itemThumbnailUri: String?,
@ColumnInfo(name = "is_in_cart")
val isInCart: Boolean
)
CustomSortShoppingListItemEntity
@Entity(
tableName = "custom_sort_shopping_list_items",
foreignKeys = [ForeignKey(
entity = ShoppingListEntity::class,
parentColumns = arrayOf("id"),
childColumns = arrayOf("shopping_list_id"),
onUpdate = ForeignKey.CASCADE,
onDelete = ForeignKey.CASCADE
)]
)
data class CustomSortShoppingListItemEntity(
@PrimaryKey(autoGenerate = true)
var id: Long = 0L,
@ColumnInfo(name = "shopping_list_id")
var shoppingListId: Long = 0L,
@ColumnInfo(name = "name")
val name: String,
@ColumnInfo(name = "category")
val category: String,
@ColumnInfo(name = "quantity")
val quantity: String,
@ColumnInfo(name = "set_quantity")
val setQuantity: String,
@ColumnInfo(name = "set_total")
val setTotal: String,
@ColumnInfo(name = "unit")
val unit: String,
@ColumnInfo(name = "item_ppu")
val itemPPU: String,
@ColumnInfo(name = "item_set_ppu")
val itemSetPPU: String,
@ColumnInfo(name = "notes")
val notes: String,
@ColumnInfo(name = "coupon_amount")
val couponAmount: String,
@ColumnInfo(name = "set_coupon_amount")
val setCouponAmount: String,
@ColumnInfo(name = "item_total")
val itemTotal: String,
@ColumnInfo(name = "item_set_total")
val itemSetTotal: String,
@ColumnInfo(name = "image_uri")
val itemImageUri: String?,
@ColumnInfo(name = "thumbnail_uri")
val itemThumbnailUri: String?,
@ColumnInfo(name = "is_in_cart")
val isInCart: Boolean
)
Shopping List Items Dao
@Dao
interface ShoppingListItemDao {
//===== SHOPPING LIST TABLE =====//
//Create
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertShoppingListItem(item: ShoppingListItemEntity)
//Read
@Query("SELECT (SELECT COUNT(*) FROM shopping_list_items WHERE shopping_list_id = :listId)")
suspend fun countListItems(listId: Long): Int
@Query("SELECT * FROM shopping_list_items WHERE shopping_list_id = :listId")
fun getAllShoppingListItems(listId: Long): PagingSource<Int, ShoppingListItemEntity>
@Query("SELECT * FROM shopping_list_items WHERE id = :id")
suspend fun getShoppingListItemById(id: Long): ShoppingListItemEntity
//Update
@Update(entity = ShoppingListItemEntity::class)
suspend fun updateShoppingListItem(item: ShoppingListItemEntity)
//Delete
@Delete
suspend fun deleteShoppingListItem(item: ShoppingListItemEntity)
//===== CUSTOM SORT TABLE =====//
//Create
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertCustomSortShoppingListItem(item: CustomSortShoppingListItemEntity)
//Read --> Throws error here
@Query("SELECT (SELECT COUNT(*) FROM custom_sort_shopping_list_items WHERE shopping_list_id = :listId)")
suspend fun countCustomSortListItems(listId: Long): Int
@Query("SELECT * FROM custom_sort_shopping_list_items WHERE shopping_list_id = :listId")
fun getAllCustomSortShoppingListItems(listId: Long): PagingSource<Int, CustomSortShoppingListItemEntity>
//Update
@Update(entity = ShoppingListItemEntity::class)
suspend fun updateCustomSortShoppingListItem(item: CustomSortShoppingListItemEntity)
//Delete
@Delete
suspend fun deleteCustomSortShoppingListItem(item: CustomSortShoppingListItemEntity)
//Update All
@Query("DELETE FROM custom_sort_shopping_list_items WHERE shopping_list_id = :listId")
suspend fun deleteAllCustomSortShoppingListItems(listId: Long)
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertAllCustomSortShoppingListItems(customItems: List<CustomSortShoppingListItemEntity>)
}