Skip to content

[Android][KSP] Generated Kotlin ORM fails to compile for auto-increment Long primary key (Operator '==' cannot be applied to 'Long' and 'Int') #1549

@Suosuo123

Description

@Suosuo123

Environment

  • WCDB 2.1.15 (com.tencent.wcdb:compiler / main / annotation)
  • Annotation processing via KSP (com.google.devtools.ksp), Kotlin 2.x, AGP 9, Android

Problem

For a Kotlin @WCDBTableCoding model with an auto-increment Long primary key, the KSP-generated ORM (DBXxxModel.kt) emits an isAutoIncrement that does not compile:

override fun isAutoIncrement(`object`: KspAutoIncProbe): Boolean {
    return `object`.databaseId == 0
}

databaseId is Long, the literal 0 is Int, and Kotlin rejects mixed-type ==:

e: .../DBKspAutoIncProbe.kt:83:10 Operator '==' cannot be applied to 'Long' and 'Int'.

So any Kotlin model with an auto-increment Long primary key fails to compile. The Java generator is unaffected because Java applies numeric promotion for long == 0; Kotlin does not allow Long == Int.

The field must stay Long (the generated setLastInsertRowId(object, lastInsertRowId: Long) assigns a Long, and rowid is 64-bit), so it cannot be worked around on the model side.

Minimal reproduction

package com.example

import com.tencent.wcdb.WCDBField
import com.tencent.wcdb.WCDBTableCoding

@WCDBTableCoding
class KspAutoIncProbe {
    @WCDBField(isPrimary = true, isAutoIncrement = true)
    var databaseId: Long = 0

    @WCDBField
    var nullableLong: Long? = null
}

Build with the KSP processor. The generated DBKspAutoIncProbe.kt contains return \object`.databaseId == 0`, which fails Kotlin compilation.

Suggested fix

Emit a Long literal in the generated comparison for Long fields, e.g. \object`.databaseId == 0L(or.toLong()), wherever the Kotlin generator compares a Long` field against an integer literal.

Question

Is this already fixed in 2.1.16 (current latest)? We are on 2.1.15 and would like to migrate our Java ORM models to Kotlin once the generated Kotlin compiles for auto-increment Long primary keys.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions