我试图将一个List [String]插入到text []类型的postgresql列中。我相信当你尝试插入任何列表时,Anorm将列表的每个成员插入到它自己的列中。我很确定这是事实,因为我找回例外:玩Anorm插入scala列表到postgres文本数组列
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
所以我使用.toArray解决了转换列表,但现在我得到“org.postgresql.util.PSQLException:找不到提供的名称VARCHAR的服务器数组类型”。它插入的列是文本[]。 – plambre
当应该传递“varchar”时,是否通过某些类型转换“VARCHAR”? – plambre
在这里你可以看到使用“VARCHAR”作为“sqlType”和jdbcType的模式https://github.com/playframework/anorm/blob/ce5e676ff0317eb3005156bd5717c768b9f8dc40/core/src/main/scala/anorm/ParameterMetaData.scala – plambre