我正在为我的ScalaTest测试用例使用测试数据库,如文档示例所示。
我有一个默认数据库和一个testdb数据库,我的Spec看起来像
class JobSpec extends FlatSpec with AutoRollback {
DBsWithEnv("test").setup('testdb)
override def db = NamedDB('testdb).toDB
override def fixture(implicit session:DBSession) = {
User.insert("test_user")
}
it should "create a new user" in { implicit session: DBSession =>
User.sqlFind("test_user") //succeeds
User.dslFind("test_user") //fails
}
}
似乎我的查询使用了sql,但使用dsl的查询却不起作用。DSL查询错误,试图访问“默认数据库”,但sql查询正确地使用了'testdb数据库。
Connection pool is not yet initialized.(name:'default)
java.lang.IllegalStateException: Connection pool is not yet initialized.(name:'default)
这是用户类
case class User(name: String)
object User extends SQLSyntaxSupport[User] {
def apply(u: SyntaxProvider[User])(rs: WrappedResultSet) = apply(u.resultName)(rs)
def apply(j: ResultName[User])(rs: WrappedResultSet) = new User(rs.get(u.name))
override val tableName = "users"
val u = User.syntax("u")
def dslFind(name: String)(implicit session: DBSession) =
withSQL {
select.from(User as u).where.eq(u.name, name)
}.map(User(u)).single().apply()
def sqlFind(name: String)(implicit session: DBSession) =
sql""" select (name) from users where name = $name;"""
.map(rs => new User(rs.string(1)).single().apply()
}
有人知道为什么在调用DSL创建的查询时,它试图使用默认数据库而不是testdb?谢谢!
发布于 2015-09-06 15:57:33
您需要这样覆盖您的SQLSyntaxSupport:
override val connectionPoolName = 'testdb
原因是SQLSyntaxSupport在第一次访问时从JDBC元数据中获取列。
http://scalikejdbc.org/documentation/sql-interpolation.html
如果您希望避免自动访问元数据,请重写列或使用autoColumns宏。
https://stackoverflow.com/questions/32404916
复制相似问题