当前位置: 首页 > 知识库问答 >
问题:

Android room迁移空错误

齐冥夜
2023-03-14

当我在Android上从旧的sqlite方式迁移到Room时,我需要使用“integer NOT null”来编译。问题是,当迁移发生时,您在新表中插入了带有“not NULL”参数的NULL字段,而我得到了错误

Android.database.sqlite.sqliteConstraintException:NOT NULL约束失败:note.notification_state(代码1299)

11-29 22:52:58.891 14605-14630/com.aleksandarvasilevski.notes E/AndroidRuntime: FATAL EXCEPTION: pool-1-thread-1
                                                                            Process: com.aleksandarvasilevski.notes, PID: 14605
                                                                            java.lang.IllegalStateException: Migration didn't properly handle note(com.aleksandarvasilevski.notes.repository.db.Note).
                                                                             Expected:
                                                                            TableInfo{name='note', columns={notification_date=Column{name='notification_date', type='TEXT', notNull=false, primaryKeyPosition=0}, priority=Column{name='priority', type='INTEGER', notNull=true, primaryKeyPosition=0}, description=Column{name='description', type='TEXT', notNull=false, primaryKeyPosition=0}, title=Column{name='title', type='TEXT', notNull=false, primaryKeyPosition=0}, id=Column{name='id', type='INTEGER', notNull=true, primaryKeyPosition=1}, notification_state=Column{name='notification_state', type='INTEGER', notNull=true, primaryKeyPosition=0}, created_date=Column{name='created_date', type='TEXT', notNull=false, primaryKeyPosition=0}}, foreignKeys=[], indices=[]}
                                                                             Found:
                                                                            TableInfo{name='note', columns={notification_date=Column{name='notification_date', type='TEXT', notNull=false, primaryKeyPosition=0}, priority=Column{name='priority', type='INTEGER', notNull=false, primaryKeyPosition=0}, title=Column{name='title', type='TEXT', notNull=false, primaryKeyPosition=0}, description=Column{name='description', type='TEXT', notNull=false, primaryKeyPosition=0}, id=Column{name='id', type='INTEGER', notNull=true, primaryKeyPosition=1}, notification_state=Column{name='notification_state', type='INTEGER', notNull=false, primaryKeyPosition=0}, created_date=Column{name='created_date', type='TEXT', notNull=false, primaryKeyPosition=0}}, foreignKeys=[], indices=[]}
                                                                                at com.aleksandarvasilevski.notes.repository.db.NoteDatabase_Impl$1.validateMigration(NoteDatabase_Impl.java:70)
                                                                                at android.arch.persistence.room.RoomOpenHelper.onUpgrade(RoomOpenHelper.java:75)
                                                                                at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.onUpgrade(FrameworkSQLiteOpenHelper.java:118)
                                                                                at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:256)
                                                                                at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
                                                                                at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableSupportDatabase(FrameworkSQLiteOpenHelper.java:93)
                                                                                at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.java:54)
                                                                                at android.arch.persistence.room.RoomDatabase.query(RoomDatabase.java:193)
                                                                                at com.aleksandarvasilevski.notes.repository.db.NoteDao_Impl$5.compute(NoteDao_Impl.java:195)
                                                                                at com.aleksandarvasilevski.notes.repository.db.NoteDao_Impl$5.compute(NoteDao_Impl.java:181)
                                                                                at android.arch.lifecycle.ComputableLiveData$2.run(ComputableLiveData.java:87)
                                                                                at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
                                                                                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
                                                                                at java.lang.Thread.run(Thread.java:761)

代码:

`@database(实体={note.class},版本=3)公共抽象类NoteDatabase扩展RoomDatabase{

public static final Migration MIGRATION_2_3 = new Migration(2, 3) {
    @Override
    public void migrate(@NonNull SupportSQLiteDatabase database) {

        database.execSQL(
                "CREATE TABLE note (id INTEGER NOT NULL, title TEXT, description TEXT, created_date TEXT, notification_date TEXT, notification_state INTEGER, priority INTEGER, PRIMARY KEY(id))");


        database.execSQL(
                "INSERT INTO note (id, title, description, created_date) SELECT _ID, title, description, date FROM notes");

    }
};`

共有1个答案

万俟英锐
2023-03-14

好的,我让它工作,如果任何人有相同的问题,只需使用NOT NULL默认值0(或其他数字)。

@Database(entities = {Note.class}, version = 3)
public abstract class NoteDatabase extends RoomDatabase {

    public static final Migration MIGRATION_2_3 = new Migration(2, 3) {
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase database) {

            database.execSQL(
                    "CREATE TABLE note (id INTEGER NOT NULL, " +
                    "title TEXT, description TEXT, created_date TEXT, notification_date TEXT, " +
                    "notification_state INTEGER NOT NULL DEFAULT 0, " +
                    "priority INTEGER NOT NULL DEFAULT 0, PRIMARY KEY(id))");
            
            database.execSQL(
                    "INSERT INTO note (id, title, description, created_date) " +
                    "SELECT _ID, title, description, date FROM notes");
            
        }
    };
    
    // ...

}
 类似资料:
  • 我用试用密钥尝试了State/BaselineMigration功能。https://flywaydb.org/documentation/concepts/baselinemigrationsFlyWay迁移执行脚本并正确移动到正确的版本。但在此基础上运行的所有迁移都失败了 从S开始2__xxx.sql然后它在版本2的模式表中创建一行并键入"SQL_STATE_SCRIPT" 再次执行flywa

  • 我将我的数据库后端从sqlite3更改为PostGRE。当我尝试运行迁移时,我有一个错误 要执行的操作:同步未迁移的应用程序:messages、avtofarm、smart_selects、staticfiles、thumbnail应用所有迁移:contenttypes、admin、callboard、auth、sessions同步未迁移的应用程序:创建表...正在运行延迟SQL...正在安装自定

  • 我的错误 我想知道做这次迁移的人打算做什么。

  • 我正在flyway的CMD中运行命令,但脚本文件的迁移会出现以下异常 [错误]无法执行目标组织。flywaydb:flyway maven插件:3.2.1:在convertopia auto db:org项目上迁移(默认cli)。flywaydb。果心应用程序编程接口。FlywayException:验证失败。迁移1.0.53的迁移描述不匹配[错误]- 我尝试过,它说构建成功,但问题仍然没有解决。

  • 当我尝试使用将android项目迁移到时,我收到一条错误消息。 错误信息 目前,以下库正在使用。 莫西 刀柄 房间 <代码>构建。渐变 有关更多详细信息,回购托管在此处-https://github.com/Abhimanyu14/finance-manager

  • 嗨,我正在尝试升级到wagtail 2.0。我已经处理了一系列错误,但我无法克服迁移时出现的错误: 非常感谢任何帮助 谢谢 提姆 完全控制台错误: (wagtail-isa) MacBook-Air:wagtailcmsdemo tim$ ./manage.py makemigrations Traceback(最近一次调用最后一次):文件“./manage.py”,第 10 行,在 execut