Hello i do hope i can ask questions here. I'm curr...
# exposed
r
Hello i do hope i can ask questions here. I'm currently facing an issue using exposed batchInsert function i keep getting the error
java.util.NoSuchElementException: List is empty.
and i'm pretty certain the list is not empty. Also please note l'm relatively new to working with ktor/exposed. I'll really appreciate any help cos it's really difficult to find helpful post or video tutorials on ktor/exposed.
Copy code
var statement: InsertStatement<Number>? = null
        var addOnStatement: List<ResultRow>?
        var foodSizeStatement: List<ResultRow>?
        var foodAddOns = emptyList<com.df.backend.model.FoodAddOn>()
        var foodSizes = emptyList<com.df.backend.model.FoodSize>()

        dbQuery {
            statement = Foods.insert { food ->
                food[name] = foodRequest.name
                food[description] = foodRequest.description
                food[category] = foodRequest.category
                food[foodImage] = foodRequest.foodImage
                food[createdAt] = foodRequest.createdAt
                food[updatedAt] = foodRequest.updatedAt
            }

            val resultRow = statement?.resultedValues?.get(0)
            println("Food count ${statement?.resultedValues?.size}")
            println("Addon List ${foodRequest.foodAddOns.size}")

            addOnStatement = FoodAddOn.batchInsert(foodRequest.foodAddOns) { addOn ->
                this[FoodAddOn.name] = addOn.name
                this[FoodAddOn.addOnPrice] = addOn.addOnPrice
                this[FoodAddOn.foodAddOnImage] = addOn.foodAddOnImage
                this[FoodAddOn.foodId] = resultRow?.get(Foods.foodId)!!
            }

            foodAddOns = rowToAddOns(addOnStatement!!)

            foodSizeStatement = FoodSize.batchInsert(foodRequest.foodSizes) { foodSize ->
                this[FoodSize.size] = foodSize.size
                this[FoodSize.sizePrice] = foodSize.sizePrice.toInt()
                this[FoodSize.foodId] = resultRow?.get(Foods.foodId)!!
            }

            foodSizes = rowToSizes(foodSizeStatement!!)
        }
        return rowToFood(statement?.resultedValues?.get(0),foodAddOns, foodSizes )
Copy code
object Foods : UUIDTable() {
    val foodId = uuid("food_id").autoGenerate().primaryKey().uniqueIndex()
    val name = varchar("name", 50).uniqueIndex()
    val foodImage = varchar("food_image", 70)
    val description = varchar("description", 500)
    val category = varchar("category", 50)
    val createdAt = varchar("created_at", 50)
    val updatedAt = varchar("updated_at", 50)
}

object FoodAddOn : UUIDTable() {
    val addOnId = uuid("food_add_on_id").autoGenerate().primaryKey()
    val name = varchar("name", 70)
    val foodAddOnImage = varchar("food_add_on_image", 70)
    val foodId = uuid("food_id")
        .references(Foods.foodId)
    val addOnPrice = integer("add_price")
    val createdAt = varchar("created_at", 50)
    val updatedAt = varchar("updated_at", 50)
}

object FoodSize : UUIDTable() {
    val foodSizeId = uuid("food_size_id").autoGenerate().primaryKey()
    val size = varchar("name", 70)
    val foodId = uuid("food_id")
        .references(Foods.foodId)
    val sizePrice = integer("size_price")
    val createdAt = varchar("created_at", 50)
    val updatedAt = varchar("updated_at", 50)
}
I'll really appreciate any form of help here 😥
j
There are some helper functions that are not provided, so I can't check all of your code. The exception provided comes from a list access on an empty list. Check around list access operations to make sure your lists are populated, and/or use kotlin nullable helpers (i.e.
firstOrNull
). Also have a gander at the Exposed wiki and make sure that your usage is correct.
UUIDTable
will automatically create the ID column for you and provides a constructor to specify the column name if desired.
r
thank you for the reply. the list is definitely not empty. i tried this also and still got the same response.
Copy code
addOnStatement = FoodAddOn.batchInsert(listOf(FoodAddOn(
                values....
            ),FoodAddOn(
                values...
            )) { addOn ->
                this[FoodAddOn.name] = addOn.name
                this[FoodAddOn.addOnPrice] = addOn.addOnPrice
                this[FoodAddOn.foodAddOnImage] = addOn.foodAddOnImage
                this[FoodAddOn.foodId] = resultRow?.get(Foods.foodId)!!
            }
and i stll got the same response
i can provide the helper functions you need
There are some helper functions that are not provided.
I have butt not much out there for exposed aside the more common insert/filtering etc queries.
Also have a gander at the Exposed wiki and make sure that your usage is correct.