android
data_error
ai_generated
true
android.database.sqlite.SQLiteException: Invalid column type (code 0): Cannot read column 'timestamp' as a long (code 0) at /path/to/RoomDatabase.kt:123
ID: android/room-invalid-cursor-query
88%Fix Rate
87%Confidence
1Evidence
2023-11-05First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| Room 2.4.0 | active | — | — | — |
| Room 2.5.0 | active | — | — | — |
| Room 2.6.0 | active | — | — | — |
| Room 2.7.0 | active | — | — | — |
| Android 8.0 | active | — | — | — |
| Android 9 | active | — | — | — |
| Android 10 | active | — | — | — |
| Android 11 | active | — | — | — |
| Android 12 | active | — | — | — |
| Android 13 | active | — | — | — |
| Android 14 | active | — | — | — |
| Android 15 | active | — | — | — |
Root Cause
Room's generated DAO implementation tries to read a column with a type that does not match the expected Java/Kotlin type, often due to a schema mismatch or incorrect converter.
generic中文
Room 生成的 DAO 实现尝试读取与预期 Java/Kotlin 类型不匹配的列,通常是由于模式不匹配或转换器错误。
Official Documentation
https://developer.android.com/training/data-storage/roomWorkarounds
-
90% success Check the entity and database version. If the column type changed, create a migration. Example: static final Migration MIGRATION_1_2 = new Migration(1, 2) { @Override public void migrate(@NonNull SupportSQLiteDatabase database) { database.execSQL("ALTER TABLE users ADD COLUMN timestamp INTEGER NOT NULL DEFAULT 0"); } }; Then add .addMigrations(MIGRATION_1_2) to the Room database builder.
Check the entity and database version. If the column type changed, create a migration. Example: static final Migration MIGRATION_1_2 = new Migration(1, 2) { @Override public void migrate(@NonNull SupportSQLiteDatabase database) { database.execSQL("ALTER TABLE users ADD COLUMN timestamp INTEGER NOT NULL DEFAULT 0"); } }; Then add .addMigrations(MIGRATION_1_2) to the Room database builder. -
85% success If the column type is correct but the converter is wrong, verify the TypeConverter. Example: @TypeConverter public static Long fromTimestamp(Long value) { return value == null ? null : value; } @TypeConverter public static Long dateToTimestamp(Long value) { return value; } Ensure both converters are symmetric and registered in @Database(entities = {...}, version = X, exportSchema = true).
If the column type is correct but the converter is wrong, verify the TypeConverter. Example: @TypeConverter public static Long fromTimestamp(Long value) { return value == null ? null : value; } @TypeConverter public static Long dateToTimestamp(Long value) { return value; } Ensure both converters are symmetric and registered in @Database(entities = {...}, version = X, exportSchema = true). -
70% success Use fallbackToDestructiveMigration() in the database builder for development: Room.databaseBuilder(context, AppDatabase.class, "db-name").fallbackToDestructiveMigration().build(); This recreates the database if no migration is found, but destroys data.
Use fallbackToDestructiveMigration() in the database builder for development: Room.databaseBuilder(context, AppDatabase.class, "db-name").fallbackToDestructiveMigration().build(); This recreates the database if no migration is found, but destroys data.
中文步骤
Check the entity and database version. If the column type changed, create a migration. Example: static final Migration MIGRATION_1_2 = new Migration(1, 2) { @Override public void migrate(@NonNull SupportSQLiteDatabase database) { database.execSQL("ALTER TABLE users ADD COLUMN timestamp INTEGER NOT NULL DEFAULT 0"); } }; Then add .addMigrations(MIGRATION_1_2) to the Room database builder.If the column type is correct but the converter is wrong, verify the TypeConverter. Example: @TypeConverter public static Long fromTimestamp(Long value) { return value == null ? null : value; } @TypeConverter public static Long dateToTimestamp(Long value) { return value; } Ensure both converters are symmetric and registered in @Database(entities = {...}, version = X, exportSchema = true).Use fallbackToDestructiveMigration() in the database builder for development: Room.databaseBuilder(context, AppDatabase.class, "db-name").fallbackToDestructiveMigration().build(); This recreates the database if no migration is found, but destroys data.
Dead Ends
Common approaches that don't work:
-
Change the field type in the entity to match the error message, e.g., change long to String
85% fail
The error indicates an actual schema mismatch; changing the type without updating the database version and migration leads to a crash or data loss.
-
Delete the app data and reinstall without running migrations
80% fail
This destroys user data and is not suitable for production; it only works if the schema was never properly migrated.
-
Add @Ignore annotation to the problematic field
90% fail
Room will ignore the field entirely, potentially breaking queries that depend on it.