[英]Play Anorm insert a scala List into a postgres text array column
我正在嘗試將 List[String] 插入到 text[] 類型的 postgresql 列中。 我相信當您嘗試插入任何 List 時,Anorm 會將 List 的每個成員插入到其自己的列中。 我很確定是這種情況,因為我得到了異常:
org.postgresql.util.PSQLException: ERROR: INSERT has more expressions than target columns
我想要做的是將整個列表作為文本 [] 插入。 我目前的代碼:
def insertList(listName: String, subLists: List[String]): Long = {
DB.withConnection{implicit c =>
SQL(
"""
INSERT INTO mailinglists(name, sublists) VALUES({listName}, {subLists})
""")
.on('listName -> listName, 'subLists -> subLists)
.executeInsert(scalar[Long] single)
}
}
我試圖走這條路:
ConnectionPool.borrow().createArrayOf("text", subLists.toArray)
但我收到錯誤:
type mismatch;
found : (Symbol, java.sql.Array)
required: anorm.NamedParameter
最終解決此問題的代碼是:
def insertList(listName: String, subLists: List[String]): Long = {
DB.withConnection{implicit c =>
SQL"INSERT INTO mailinglists(name, sublists) VALUES($listName, ARRAY[$subLists])"
.executeInsert(scalar[Long] single)
}
}
與原始帖子相比,更改的是使用 Anorm 字符串插值 SQL"..." 並在多值參數周圍添加ARRAY[...]
。 我不確定為什么這會起作用,因為 postgres 異常非常神秘。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.