我正在迁移我的Room数据库。我想添加新表。所以我创建了这样的Entry类:
@Entity(foreignKeys = {@ForeignKey(entity = Project.class,
parentColumns = "projectId",
childColumns = "projectId",
onDelete = ForeignKey.CASCADE)},
indices = {
@Index(name = "projectId_index", value = {"projectId"})
})
public class ProjectDimension {
@PrimaryKey(autoGenerate = true)
private long dimensionId;
@ColumnInfo
private long projectId;
@ColumnInfo
private String name;
@ColumnInfo
private String value;
// getters and setters here...
}
然后我的Dao看起来像这样:
@Dao
public interface ProjectDimensionDao {
@Query("SELECT * FROM ProjectDimension")
Single<List<ProjectDimension>> getAll();
@Query("SELECT * FROM ProjectDimension WHERE projectId = :projectId")
Single<List<ProjectDimension>> getByProject(long projectId);
@Insert(onConflict = OnConflictStrategy.REPLACE)
long insert(ProjectDimension projectDimension);
@Delete
void delete(ProjectDimension projectDimension);
}
最后是我的数据库类:
@Database(entities = {
Contact.class,
ContactEmail.class,
ContactPhone.class,
Monitoring.class,
Organization.class,
OrgEmail.class,
OrgPhone.class,
Project.class,
ProjectContact.class,
ProjectLocation.class,
ProjectDimension.class
}, version = 2)
public abstract class MonitoringDatabase extends RoomDatabase {
private static MonitoringDatabase instance;
// other Data Access Objects (DAO) here...
public abstract ProjectDimensionDao projectDimensionDao();
public static MonitoringDatabase getInstance(Context context) {
if (instance == null) {
instance = Room.databaseBuilder(context.getApplicationContext(),
MonitoringDatabase.class, "monitoring-database")
.addMigrations(MIGRATION_1_2)
.build();
}
return instance;
}
/**
* Upgrade database from version 1 to 2.
* Details: Added new table named ProjectDimension
*/
private static final Migration MIGRATION_1_2 = new Migration(1, 2) {
@Override
public void migrate(@NonNull SupportSQLiteDatabase database) {
// create ProjectDimension table
database.execSQL("CREATE TABLE `ProjectDimension` (`dimensionId` INTEGER, `projectId` INTEGER, " +
"`name` TEXT, `value` TEXT, " +
"PRIMARY KEY(`dimensionId`), " +
"FOREIGN KEY(`projectId`) REFERENCES `Project`(`projectId`) ON DELETE CASCADE)");
}
};
public static void destroyInstance() {
instance = null;
}
}
运行后,我收到一个如下错误:
Expected:
TableInfo{name='ProjectDimension', columns={name=Column{name='name', type='TEXT', notNull=false, primaryKeyPosition=0}, value=Column{name='value', type='TEXT', notNull=false, primaryKeyPosition=0}, projectId=Column{name='projectId', type='INTEGER', notNull=true, primaryKeyPosition=0}, dimensionId=Column{name='dimensionId', type='INTEGER', notNull=true, primaryKeyPosition=1}}, foreignKeys=[ForeignKey{referenceTable='Project', onDelete='CASCADE', onUpdate='NO ACTION', columnNames=[projectId], referenceColumnNames=[projectId]}], indices=[Index{name='projectId_index', unique=false, columns=[projectId]}]}
Found:
TableInfo{name='ProjectDimension', columns={name=Column{name='name', type='TEXT', notNull=false, primaryKeyPosition=0}, value=Column{name='value', type='TEXT', notNull=false, primaryKeyPosition=0}, projectId=Column{name='projectId', type='INTEGER', notNull=false, primaryKeyPosition=0}, dimensionId=Column{name='dimensionId', type='INTEGER', notNull=false, primaryKeyPosition=1}}, foreignKeys=[ForeignKey{referenceTable='Project', onDelete='CASCADE', onUpdate='NO ACTION', columnNames=[projectId], referenceColumnNames=[projectId]}], indices=null}
我认为问题出在我的CREATE语句中。我一直在为此搜索正确的SQL查询,但仍未找到一些。有人帮忙!
答案 0 :(得分:2)
您有2个错误,一个与NOT NULL列属性有关,另一个与外键有关。
这是区别:
预期:
projectId notNull = true
dimensionId notNull = true
foreignKeys索引= [索引{name ='projectId_index',unique = false,列= [projectId]}]}
找到
projectId notNull = false
dimensionId notNull = 否
foreignKeys索引= 空
我可以为您解决第一个问题,在您的实体中,类型long
不能为空。将其更改为Long
以使其正常工作。
对于第二个,在您的迁移函数中添加类似的内容
database.execSQL("CREATE INDEX projectId_index ON Project (projectId)");
答案 1 :(得分:0)
您需要将projectId标注为@nonNull注释。