Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SqliteDbType.insertReturning method throw NoSuchElementException: None.get #87

Open
polkx opened this issue Dec 18, 2024 · 2 comments · May be fixed by #104
Open

SqliteDbType.insertReturning method throw NoSuchElementException: None.get #87

polkx opened this issue Dec 18, 2024 · 2 comments · May be fixed by #104

Comments

@polkx
Copy link

polkx commented Dec 18, 2024

I get an error when running the following piece of code.

From what I managed to deduce, a piece of code is executed in object SqliteDbType in the insertReturning method (val id = idCodec.readSingle(rs) ) and instead of the ID it returns the number of the created row.

Code Sample

//> using scala 3.6.2
//> using dep com.augustnagro::magnum:1.3.1
//> using dep com.zaxxer:HikariCP:6.2.1
//> using dep org.xerial:sqlite-jdbc:3.47.1.0

//CREATE TABLE test (
//  id TEXT PRIMARY KEY,
//  value INTEGER NOT NULL
//);

import com.augustnagro.magnum.*
import com.zaxxer.hikari.HikariDataSource

@SqlName("test")
@Table(SqliteDbType, SqlNameMapper.CamelToSnakeCase)
case class TestRow(id: String, value: Int)

class TestRepo extends Repo[TestRow, TestRow, String]

@main
def main(): Unit =
  println("Hello world!")
  val ds = HikariDataSource()
  ds.setJdbcUrl("jdbc:sqlite:database.db")

  transact(ds):
    new TestRepo()
    .insertReturning(TestRow(id = "testId", value = 1))

Stacktrace:

Hello world!
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" com.augustnagro.magnum.SqlException: Error executing query:
INSERT INTO test (id, value) VALUES (?, ?)
SELECT * FROM test WHERE id = ?
With message:
java.util.NoSuchElementException: None.get

	at com.augustnagro.magnum.util$package$.handleQuery(util.scala:350)
	at com.augustnagro.magnum.SqliteDbType$$anon$1.insertReturning(SqliteDbType.scala:158)
	at com.augustnagro.magnum.Repo.insertReturning(Repo.scala:44)
	at main$package$.main$$anonfun$1(main.scala:29)
	at com.augustnagro.magnum.util$package$.transact$$anonfun$1(util.scala:38)
	at scala.util.Using$.resource(Using.scala:296)
	at com.augustnagro.magnum.util$package$.transact(util.scala:44)
	at com.augustnagro.magnum.util$package$.transact(util.scala:47)
	at main$package$.main(main.scala:28)
	at main.main(main.scala:19)
Caused by: java.util.NoSuchElementException: None.get
	at scala.None$.get(Option.scala:627)
	at scala.None$.get(Option.scala:626)
	at com.augustnagro.magnum.SqliteDbType$$anon$1.insertReturning$$anonfun$1$$anonfun$1(SqliteDbType.scala:158)
	at com.augustnagro.magnum.util$package$.timed(util.scala:193)
	at com.augustnagro.magnum.SqliteDbType$$anon$1.insertReturning$$anonfun$1(SqliteDbType.scala:158)
	at scala.util.Using$Manager.scala$util$Using$Manager$$manage(Using.scala:202)
	at scala.util.Using$Manager$.apply(Using.scala:255)
	... 9 more
@riccardomerolla
Copy link

Maybe it can be helpful, I read that SQLite has this RETURNING function that can be used to retrieve the ID or the full row values

https://www3.sqlite.org/lang_returning.html

val insertSql =
  s"INSERT INTO $tableNameSql $ecInsertKeys VALUES (${ecCodec.queryRepr}) RETURNING $idName"

@AugustNagro
Copy link
Owner

Sorry for the delay @polkx & @riccardomerolla , was away for the holidays. When implementing insertReturning, I (sanely) assumed that it was returning the generated integer ID. However, that does not appear to be true.

The xerial/sqlite-jdbc driver doesn't appear to support this usecase at all:

xerial/sqlite-jdbc#1061
xerial/sqlite-jdbc#1186

Is there another sqlite driver you prefer that we can replace it with? Worst case, I can make these methods just throw UnsupportedOperationException.

@AugustNagro AugustNagro linked a pull request Feb 2, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants