Thread
#exposed
    d

    degreat

    2 years ago
    INSERT IGNORE...
    throws an exception when using H2. I'm up-to-date with exposed and h2 drivers... Am I missing anything else?
    t

    Tristan Blakers

    2 years ago
    fwiw, I use these extension functions with Exposed. Cribbed with a modificaiton or two from a github repo. This is on Exposed 24.1, may be a better way by now
    class InsertOrIgnore<Key : Any>(
        table: Table,
        isIgnore: Boolean = false,
        private vararg val keys: Column<*>
    ) : InsertStatement<Key>(table, isIgnore) {
    
        override fun prepareSQL(transaction: Transaction): String {
            val tm = TransactionManager.current()
            val onConflict = "ON CONFLICT DO NOTHING"
            return "${super.prepareSQL(transaction)} $onConflict"
        }
    }
    
    fun <T : Table> T.insertOrIgnore(vararg keys: Column<*>, body: T.(InsertStatement<Number>) -> Unit) =
        InsertOrIgnore<Number>(this, keys = keys).apply {
            body(this)
            execute(TransactionManager.current())
        }
    tapac

    tapac

    2 years ago
    @degreat "INSERT IGNORE" supported only in MySQL mode on H2. Please check http://www.h2database.com/html/features.html "MySQL Compatibility Mode" section.