Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

634
Vistas
Android Room notNull field is expecting a null default value

i'm trying to add new attributes to my tables as shown here:


@Entity(
    tableName = "invoices", indices = [Index(value = [...], unique = true)]
)
data class Invoice(
    ...
    override val created_at: Long = 0,
    override var approved_at: Long? = 0,
) : ICD

so i created the following migration:

val MIGRATION_3_4 = object: Migration(3, 4) {
            override fun migrate(database: SupportSQLiteDatabase) {
                database.execSQL("ALTER TABLE invoices ADD COLUMN created_at INTEGER NOT NULL DEFAULT 0")
                database.execSQL("ALTER TABLE invoices ADD COLUMN approved_at INTEGER DEFAULT 0 ")
            }
        }

but whenever i try to run it says the migration wasnt handled properly and shows this as expected:

TableInfo{name='invoices', columns={... ,created_at=Column{name='created_at', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}, approved_at=Column{name='approved_at', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}}, ... }

and this as whats found:

TableInfo{name='invoices', columns={... ,created_at=Column{name='created_at', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='0'}, approved_at=Column{name='approved_at', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='0'}}, ... }
        

can anyone tell me why is it expecting default value null please?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

What I believe is, you are using Room version greater than 2.2.0, and version 2.2.0 and above needs default values of the columns to be defined in their respective entity class using @ColumnInfo(defaultValue="0")

For your case add @ColumnInfo(defaultValue="0") to created_at and approved_at

@ColumnInfo(defaultValue="0")
override val created_at: Long = 0,
@ColumnInfo(defaultValue="0")
override var approved_at: Long? = 0,

And change your migration function to this

database.execSQL("ALTER TABLE invoices ADD COLUMN created_at INTEGER NOT NULL DEFAULT 0")
database.execSQL("ALTER TABLE invoices ADD COLUMN approved_at INTEGER")

If you're using a Room version 2.4.0-alpha01 and higher, you can use autoMigration.

For autoMigration, add autoMigrations annotation to your database

@Database(
    entities = [.., .., ..],
    version = 2,
    autoMigrations = [AutoMigration(from = 1, to = 2)]
)
abastract class Database: RoomDatabase
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda