Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

633
Views
El campo Android Room notNull espera un valor predeterminado nulo

Estoy tratando de agregar nuevos atributos a mis tablas como se muestra aquí:

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

así que creé la siguiente migración:

 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 ") } }

pero cada vez que intento ejecutarlo, dice que la migración no se manejó correctamente y muestra esto como se esperaba:

 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'}}, ... }

y esto como lo que se encuentra:

 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'}}, ... }

¿Alguien puede decirme por qué espera un valor predeterminado nulo, por favor?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Lo que creo es que está utilizando una versión de Room superior a 2.2.0 , y la versión 2.2.0 y superior necesita que los valores predeterminados de las columnas se definan en su clase de entidad respectiva usando @ColumnInfo(defaultValue="0")

Para su caso, agregue @ColumnInfo(defaultValue="0") a created_at y approved_at

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

Y cambie su función de migración a esta

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

Si usa una versión de Room 2.4.0-alpha01 y superior, puede usar autoMigration .

Para autoMigration , agregue la anotación de autoMigrations a su base de datos

 @Database( entities = [.., .., ..], version = 2, autoMigrations = [AutoMigration(from = 1, to = 2)] ) abastract class Database: RoomDatabase
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!