gpt4 book ai didi

java - 房间用索引创建表

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:00:22 25 4
gpt4 key购买 nike

我正在迁移我的 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
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 查询,但仍然找不到。有人帮忙!

最佳答案

您有 2 个错误,一个关于 NOT NULL 列属性,另一个关于外键。

区别如下:

预期:

projectId notNull=true

dimensionId notNull=true

foreignKeys indices=[Index{name='projectId_index', unique=false, columns=[projectId]}]}

找到

projectId notNull=false

dimensionId notNull=false

foreignKeys indices=null

我可以帮助解决第一个问题,在您的实体中,long 类型不能为空。将其更改为 Long 以使其工作。

对于第二个,将类似的内容添加到您的迁移函数中

database.execSQL("CREATE INDEX projectId_index ON Project (projectId)");

关于java - 房间用索引创建表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49419520/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com