yuweiguocn / GreenDaoUpgradeHelper

To solve database upgrade of greenDao and just need one line code
1.53k stars 295 forks source link

UNIQUE constraint failed #34

Closed JohnsonZZZ closed 7 years ago

JohnsonZZZ commented 7 years ago

大佬你好,我现在引用了你最新的库文件之后,修改表字段,出现了这个问题。 【Failed to restore data from temp table 】BlackList_TEMP android.database.sqlite.SQLiteConstraintException: UNIQUE constraint failed: BlackList.PKBlackList (code 1555)

下面是我的model类,项目中采用UUID作为唯一区分,没有long类型id,并且转换为byte存到数据库中。不知道问题出现在哪里,求指教。

@Entity(nameInDb = "BlackList", createInDb = false) public class BlackListEntity {

@Property(nameInDb = "PKBlackList")
@Convert(converter = UUID2BytesConverter.class, columnType = byte[].class)
public UUID PKBlackList;

@Property(nameInDb = "PKCompany")
@Convert(converter = UUID2BytesConverter.class, columnType = byte[].class)
public UUID PKCompany;

@Property(nameInDb = "Cellphone")
public String Cellphone;

@Property(nameInDb = "Description")
public String Description;

@Property(nameInDb = "DeleteStatus")
public int DeleteStatus;

@Property(nameInDb = "PKUser")
@Convert(converter = UUID2BytesConverter.class, columnType = byte[].class)
public UUID PKUser;

@Property(nameInDb = "CreateTime")
public String CreateTime;

@Property(nameInDb = "TestValu")
public String TestValu;

@Generated(hash = 465873519)
public BlackListEntity(UUID PKBlackList, UUID PKCompany, String Cellphone, String Description,
        int DeleteStatus, UUID PKUser, String CreateTime, String TestValu) {
    this.PKBlackList = PKBlackList;
    this.PKCompany = PKCompany;
    this.Cellphone = Cellphone;
    this.Description = Description;
    this.DeleteStatus = DeleteStatus;
    this.PKUser = PKUser;
    this.CreateTime = CreateTime;
    this.TestValu = TestValu;
}

@Generated(hash = 1391692307)
public BlackListEntity() {
}

}

下面是我的OpenHelper @Override public void onUpgrade(Database db, int oldVersion, int newVersion) { LogUtil.e("MigrationHelper", "from oldVersion:" + oldVersion + "to newVersion:" + newVersion); MigrationHelper.migrate(db, new MigrationHelper.ReCreateAllTableListener() { @Override public void onCreateAllTables(Database db, boolean ifNotExists) { DaoMaster.createAllTables(db, ifNotExists); } @Override public void onDropAllTables(Database db, boolean ifExists) { DaoMaster.dropAllTables(db, ifExists); } }, BlackListEntityDao.class); }

yuweiguocn commented 7 years ago

请查看生成的Dao类中的SQL语句,是由于该字段添加了唯一键约束导致,请尝试移除唯一键约束解决此问题。

JohnsonZZZ commented 7 years ago

如果是之前的项目添加了唯一键约束,然后新版本里面使用了这个框架,并且移除了唯一键约束,也会出现该问题,请问大佬有什么解决思路吗? 我这边先把用户的数据库删除了,然后新建 就好了 但是这样的操作显得有点不够友好。

JohnsonZZZ commented 7 years ago

@yuweiguocn

JohnsonZZZ commented 7 years ago

已解决