`INSERT IGNORE...` throws an exception when using ...
# exposed
d
INSERT IGNORE...
throws an exception when using H2. I'm up-to-date with exposed and h2 drivers... Am I missing anything else?
t
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
Copy code
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())
    }
t
@degreat "INSERT IGNORE" supported only in MySQL mode on H2. Please check http://www.h2database.com/html/features.html "MySQL Compatibility Mode" section.
345 Views