android room not null约束插入失败

时间:2018-07-08 07:51:08

标签: php android retrofit android-room

我在Android应用中使用翻新作为api和数据库访问空间。从我的后端收到的数据看起来像这样

[{"category_id":8},{"category_name":"Appetizers"},{"category_image":"upload\/images\/1229-2016-08-30.jpg"},{"category_id":5},{"category_name":"Breakfast"},{"category_image":"upload\/images\/6307-2016-08-30.jpg"},{"category_id":2},{"category_name":"Desserts"},{"category_image":"upload\/images\/0062-2016-08-30.jpg"},{"category_id":1},{"category_name":"Drink"},{"category_image":"upload\/images\/9359-2016-08-30.jpg"},{"category_id":4},{"category_name":"Main Dish"},{"category_image":"upload\/images\/2031-2016-08-30.jpg"},{"category_id":3},{"category_name":"Side Dish"},{"category_image":"upload\/images\/3824-2016-08-30.jpg"}]

我的房间实体看起来像这样

@Entity(primaryKeys = ["category_id"])
data class MenuCategories(
        @field:SerializedName("category_id")
        val category_id: String,

        @field:SerializedName("category_name")
        val category_name: String,

        @field:SerializedName("category_image")
        val category_image: String
)

以及我的dao如何保存数据

@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertMenuCategories(categories: List<MenuCategories>)

但是我收到此错误消息

E/SQLiteLog: (1299) abort at 8 in [INSERT OR REPLACE INTO `MenuCategories`(`category_id`,`category_name`,`category_image`) VALUES (?,?,?)]: NOT NULL constraint failed: MenuCategories.category_name

1 个答案:

答案 0 :(得分:1)

您需要像下面这样在一个JsonObject中添加category_idcategory_namecategory_image

[
    {
      "category_id": 8,
      "category_name": "Appetizers",
      "category_image": "upload\/images\/1229-2016-08-30.jpg"
    }, {
       .....
    }
]