Home > Software design >  Prepopulating database with room creates database with wrong schema
Prepopulating database with room creates database with wrong schema

Time:10-02

I need a prepopulated database in my app. Room correctly creates the database; but when I try to interact with it; the app crashes with Pre-packaged database has an invalid schema error. The schema of the generated file (from data/data/com.myapp/databases/) folder seems to be fine. I am using DB Browser Lite to visually create my database.

MESSAGE -- Pre-packaged database has an invalid schema: EnglishMarathi(com.myapp.data.EnglishMarathi).
     Expected:
    TableInfo{name='EnglishMarathi', columns={id=Column{name='id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}, word=Column{name='word', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, meaning3=Column{name='meaning3', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, meaning2=Column{name='meaning2', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, meaning1=Column{name='meaning1', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]}
     Found:
    TableInfo{name='EnglishMarathi', columns={englishTyped=Column{name='englishTyped', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, id=Column{name='id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'}, word=Column{name='word', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}, meaning3=Column{name='meaning3', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, meaning2=Column{name='meaning2', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'}, meaning1=Column{name='meaning1', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]} 
CAUSE -- null 
TRACE -- [Ljava.lang.StackTraceElement;@450ced0

Update 1

I have tried creating a small database file from scratch from the schema.json sql script. Still the issue is there.

{...
"tableName": "MarathiEnglish",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER NOT NULL, `word` TEXT NOT NULL, `meaning1` TEXT NOT NULL, `meaning2` TEXT, `meaning3` TEXT, `englishTyped` TEXT, PRIMARY KEY(`id`))",
...}

CodePudding user response:

With Room it is the @Entity class that defines/determines the table that Room expects.

Your issue, according to the messages is that the Entity does not include the englishTyped column that is in the pre-populated database.

So you need to either

  • add the englishTyped column to the EnglishMarathi Entity (class)
    • possibly val englishTyped: String or
  • remove the englishTyped column from the pre-populated database.

I have tried creating a small database file from scratch from the schema.json sql script. Still the issue is there.

The simpler way is to

  1. create the Entity,
  2. compile the project (Ctrl F9) and
  3. using the Android View in Android Studio, find and expand the Java (generated) in the Project Explorer Window.
  4. Then expand the sub directorys and find the class that is the same name, but suffixed with _Impl, as the class that you have annotated with @Database.
  5. within that class there will be a method named createAllTables, the EXPECTED SQL for the table(s) can be copied and pasted into DB Browser.

Example

Say you have the EnglishMarathi class as :-

@Entity
data class EnglishMarathi(
    @PrimaryKey
    val id: Long,
    @NonNull
    val word: String,
    val meaning1: String,
    val meaning2: String?,
    val meaning3: String?,
    val englishTyped: String
)

and your @Database annotated class is TheDatabase, then after compiling the project :-

enter image description here

  • Related