【发布时间】:2019-08-18 07:43:55
【问题描述】:
我想通过 Slick 在 SQLite 中启用外键验证。我正在使用 Slick 3.3.0。我该怎么做?
目前我正在通过 DatabaseConfig[SQLiteProfile] 连接到 SQLite,具体做法是
DatabaseConfig.forConfig(path = configKey, classLoader = getClass.getClassLoader)
我的配置如下所示:
{
"dataSourceClass": "slick.jdbc.DatabaseUrlDataSource",
"db": {
"driver": "org.sqlite.JDBC",
"properties": {
"foreign_keys": true
},
"url": "jdbc:sqlite:/path/to/mydb.sqlite?foreign_keys=on"
},
"profile": "slick.jdbc.SQLiteProfile$"
}
我尝试将?foreign_keys=ON 添加到我的 JDBC URL 的末尾。我还尝试将properties 对象移出db 对象并移至根级别。
如果我直接通过 JDBC 与数据库交互,我就能让它工作:
package test
import java.sql.DriverManager
object Main extends App {
val connection = DriverManager.getConnection(
"jdbc:sqlite:/path/to/mydb.sqlite?foreign_keys=on")
val statement = connection.createStatement()
// this line throws, because table_with_fk is a table
// with foreign keys into a different table
statement.executeUpdate(
"insert into table_with_fk values (0, 0, 0, 0, 0, 0, 0, 0)")
}
【问题讨论】: