简体   繁体   中英

android room database schema invalid error caused by primaryKeyPosition

I have a simple database with few tables.i have implemented room entities for tables.i'm getting this error and it's saying that my schema is invalid.but i found it very similar only difference is column order and primaryKeyPosition.

error message(i have omitted the foreign keys as they are equal)

Expected:

TableInfo{name='am',
 columns={name=Column{name='name', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'},
 symbol=Column{name='symbol', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'},
 a_number=Column{name='a_number', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'},
 a_id=Column{name='a_id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}},


Found:

TableInfo{name='am',
 columns={symbol=Column{name='symbol', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'},
 name=Column{name='name', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='null'},
 a_number=Column{name='a_number', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='null'},
 a_id=Column{name='a_id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=2, defaultValue='null'}},

i have attached a image to clarify the difference

在此处输入图像描述

as you can see only differences are * 1st and 2nd table column order is different * primaryKeyPosition is different in last column

my entity class

@Entity(tableName = "am",
        foreignKeys = {
                @ForeignKey(entity = ATypes.class,
                        parentColumns = "a_id",
                        childColumns = "a_id"),
        },
        indices = {@Index("a_id")}
)
public class Am {

    public String name;
    public String symbol;
    @PrimaryKey
    @ColumnInfo(name = "a_number")
    public int aNumber;

    @NonNull
    @ColumnInfo(name = "a_id")
    public Integer aId;

}

my question how can i change the column order or primaryKeyPosition in order to match expected schema.

You are missing the index for your primary key!

If you are using Robolectric the problem may be caused by this issue https://github.com/robolectric/robolectric/issues/4209

I had the same issues and I solved it with https://github.com/guness/RobolectricSQLite

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM