Migration didn't properly handle: appointment(com.example.spaappointment.data.Appointment)

73 views Asked by At

I had this error while my Appointment class is

package com.example.spaappointment.data

import androidx.annotation.NonNull
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey

@Entity(tableName = "appointment")
data class Appointment(
@NonNull @PrimaryKey
val date: String,

@ColumnInfo
val name: String,

@NonNull
@ColumnInfo
val phonenumber: Int,

@NonNull
@ColumnInfo
val email: String,

@ColumnInfo
val note: String

)

and the Jason file looks like this

{   "formatVersion": 1,   "database": {     "version": 3,     "identityHash": "939188bd3f343aa9fe8319733e41e36a",     "entities": [       {         "tableName": "service",         "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`price`TEXT NOT NULL,`name`TEXT NOT NULL,`description`TEXT NOT NULL,`id` INTEGER NOT NULL, PRIMARY KEY(`id`))",         "fields": [           {             "fieldPath": "price",             "columnName": "price",             "affinity": "TEXT",             "notNull": true           },           {             "fieldPath": "name",             "columnName": "name",             "affinity": "TEXT",             "notNull": true           },           {             "fieldPath": "description",             "columnName": "description",             "affinity": "TEXT",             "notNull": true           },           {             "fieldPath": "id",             "columnName": "id",             "affinity": "INTEGER",             "notNull": true           }         ],         "primaryKey": {           "columnNames": [             "id"           ],           "autoGenerate": false         },         "indices": [],         "foreignKeys": []       },       {         "tableName": "appointment",         "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`date`TEXT NOT NULL,`name`TEXT NOT NULL,`phonenumber`INTEGER NOT NULL,`email`TEXT NOT NULL,`note` TEXT NOT NULL, PRIMARY KEY(`date`))",         "fields": [           {             "fieldPath": "date",             "columnName": "date",             "affinity": "TEXT",             "notNull": true           },           {             "fieldPath": "name",             "columnName": "name",             "affinity": "TEXT",             "notNull": true           },           {             "fieldPath": "phonenumber",             "columnName": "phonenumber",             "affinity": "INTEGER",             "notNull": true           },           {             "fieldPath": "email",             "columnName": "email",             "affinity": "TEXT",             "notNull": true           },           {             "fieldPath": "note",             "columnName": "note",             "affinity": "TEXT",             "notNull": true           }         ],         "primaryKey": {           "columnNames": [             "date"           ],           "autoGenerate": false         },         "indices": [],         "foreignKeys": []       }     ],     "views": [],     "setupQueries": [       "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",       "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '939188bd3f343aa9fe8319733e41e36a')"     ]   } }

and this is the error:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.spaappointment, PID: 9952
java.lang.IllegalStateException: Migration didn't properly handle: appointment(com.example.spaappointment.data.Appointment).
Expected:
TableInfo{name='appointment', columns={date=Column{name='date', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=1, defaultValue='null'}, name=Column{name='name', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, phonenumber=Column{name='phonenumber', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}, note=Column{name='note', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, email=Column{name='email', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]}
Found:
TableInfo{name='appointment', columns={}, foreignKeys=[], indices=[]}

I don't know how to fix it.

1

There are 1 answers

0
MikeT On

Room is a little fickle in regards to what it expects and it's not the easiest task to ascertain what the difference is between the expected and found schemas (CREATE TABLE SQL) in the log when the Migration fails.

However, there is no need, as Room can tell you exactly what is expected.

What you do is

  1. create the new/changed @Entity annotated classes
  2. include them, if not already included in the entities parameter of the @Database annotation.
  3. Compile the project (Ctrl + F9)
  4. Locate the java(generated) (visible via the Android View)
  5. Find the class that is the same name as the @Database annotated class but suffixed with _Impl
  6. Find the createAllTables method and then you have the SQL that Room would use to create the tables (ignore room_master which holds the hash).
  7. Base the migration on the respective SQL.

Saying that the found (i.e. what is actually in the database after the Migration has been run) being

Found: TableInfo{name='appointment', columns={}, foreignKeys=[], indices=[]}

Indicates that there are no columns, foreign keys or indexes for the appointment table. Are you sure that you have created the appointment table in the migration? If not then you need to and you can use the SQL from the generated java as indicated previously.