在 Slick

问题描述

我有如下声明

  def create(company: Company): Future[Company] = {
    dbConfig.db.run(companies.filter(_.name === company.name).result.headOption)
}

并且它们会因诸如此类的异常而失败

org.h2.jdbc.JdbcsqlSyntaxErrorException: Syntax error in sql statement

我试图逃避单引号,但没有成功。

dbConfig.db.run(companies.filter(_.name === company.name.replace("'","''")).result.headOption)

当我尝试插入这样的记录时:

val company = doSync(companies.create(Company(0,"C's company")))

我得到的例外是:

Syntax error in sql statement "select `id`,`name` from `company` where `name` = 'C\'s company[*]'"; sql statement:
select `id`,`name` from `company` where `name` = 'C\'s company' [42000-200]
org.h2.jdbc.JdbcsqlSyntaxErrorException: Syntax error in sql statement "select `id`,`name` from `company` where `name` = 'C\'s company' [42000-200]

请注意,我在 MysqL 模式下使用 H2 来运行我的测试。

解决方法

该错误向我表明您导入了错误的 Slick 配置文件。我不熟悉 Mysql 模式下的 H2。也许您需要使用 MySQL 配置文件? (如果您使用的是 MySQL,则为 H2)