Architecture-components-samples: how to Migration old nullable table column

Created on 31 Oct 2017  路  4Comments  路  Source: android/architecture-components-samples

I am trying to support room db component but one crash I can not find any useful answer:
The crash log just like:

 java.lang.RuntimeException: Unable to resume activity 
: java.lang.IllegalStateException: Migration didn't properly handle search_record(entity.user.SearchRecord).
                                                                     Expected:
                                                                    TableInfo{name='search_record', columns={_id=Column{name='_id', type='INTEGER', notNull=true, primaryKeyPosition=1}, search_num=Column{name='search_num', type='INTEGER', notNull=true, primaryKeyPosition=0}, key_word=Column{name='key_word', type='TEXT', notNull=false, primaryKeyPosition=0}, recent_date=Column{name='recent_date', type='INTEGER', notNull=true, primaryKeyPosition=0}}, foreignKeys=[], indices=[]}
                                                                     Found:
                                                                    TableInfo{name='search_record', columns={_id=Column{name='_id', type='INTEGER', notNull=false, primaryKeyPosition=1}, search_num=Column{name='search_num', type='INTEGER', notNull=false, primaryKeyPosition=0}, key_word=Column{name='key_word', type='TEXT', notNull=false, primaryKeyPosition=0}, recent_date=Column{name='recent_date', type='INTEGER', notNull=false, primaryKeyPosition=0}}, foreignKeys=[], indices=[]}

It seems that the old db table created before is designed notNull = false , but the room only allow notNull = true int column . so is there any way for me to migration the table without delete table and recreate a new one.

Most helpful comment

Today i faced this issue too. But, after spending some time in thinking i came up with this little idea which solved the issue. So, the idea is to use data type as Integer instead of int. int is a primitive data-type so it cant be null but Integer is a class so object of this class can be null.

All 4 comments

@george5613 I met this problem too, I updated room to 1.0.0-rc1, when I add new columns to a table, it just crashed , and I found this for a while , old nullable column must be not null , but how can I do in migrations?

-id=Column{name='id', type='TEXT', notNull=true, primaryKeyPosition=1},
+id=Column{name='id', type='TEXT', notNull=false, primaryKeyPosition=1},

-time_stamp=Column{name='time_stamp', type='INTEGER', notNull=true, primaryKeyPosition=0},
+time_stamp=Column{name='time_stamp', type='INTEGER', notNull=false, primaryKeyPosition=0},

-state=Column{name='state', type='INTEGER', notNull=true, primaryKeyPosition=0},
+state=Column{name='state', type='INTEGER', notNull=false, primaryKeyPosition=0}

@yaraki Since the part of room component annotationProcessor SDK is not open source At this moment. The only way to solve this problem is follow the way in the sample :
1.Create a tmp table
2.Insert old rows to the tmp table
3.Drop old table
4.Rename the tmp table the same as the old one.
Maybe it has another simple solution but for me it's the only way I can find now.

Today i faced this issue too. But, after spending some time in thinking i came up with this little idea which solved the issue. So, the idea is to use data type as Integer instead of int. int is a primitive data-type so it cant be null but Integer is a class so object of this class can be null.

@florina-muntenescu was this closed because @george5613 comment is the advised/recommended solution?

Was this page helpful?
0 / 5 - 0 ratings