播放框架-SQLite:启用外键 [英] play framework - SQLite: Enable Foreign Key

查看:132
本文介绍了播放框架-SQLite:启用外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将SQLite与Play一起使用时遇到问题,SQLite文档说默认情况下,外键是禁用的,并且需要为每个连接启用.

I have a problem when using SQLite with play, SQLite documentation says that foreign keys are disabled by default, and need to be enabled with each connection.

我正在将SQLite与Ebean一起使用,在进化中,我添加了以下行:

I am using SQLite with Ebean, in evolution I added this line:

PRAGMA foreign_keys = ON;

但是它不起作用,仅当我从终端或SQLite客户端打开连接并执行该行(PRAGMA foreign_keys = ON;)时才起作用,我也在应用程序的开头尝试了此操作,但是没有运气:

but It doesn't work, it only works when I open a connection from terminal or SQLite Client and execute that line (PRAGMA foreign_keys = ON;), I also tried this in the start of the app but with no luck:

Ebean.getServer("mom").createSqlUpdate("PRAGMA foreign_keys = ON;").execute();

那么,如何启用外键?

推荐答案

由于PRAGMA与连接而不是数据库无关,因此您不能通过演进来做到这一点.

You cannot do this via an evolution, as the PRAGMA is tied to a connection, and not to the database.

由于类似原因,在启动时无法调用SQL:它将仅对运行查询的池中的连接启用PRAGMA.

Calling the SQL on startup will not work for similar reasons: it will enable the PRAGMA only for the connection from the pool you happen to run the query on.

您需要为正在使用的每个连接进行此操作.

You need to do it for every connection that is being used.

根据 Play框架的JDBC文档,您可以通过以下方法实现此目的配置连接池以对application.conf文件中的每个新连接执行PRAGMA:

According to the JDBC documentation of Play Framework, you can achieve this by configuring the connection pool to execute the PRAGMA for every new connection in the application.conf file:

db.default.initSQL="PRAGMA foreign_keys = true"

您可以将连接/服务器名称default替换为mom或配置JDBC连接的方式.

You can substitute the connection/server name default with mom or how ever you configured your JDBC connection.

这篇关于播放框架-SQLite:启用外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆