如何使用 Dropwizard 和 JDBI 为 SQLite 启用外键约束?

How do I enable foreign key constraints for SQLite with Dropwizard and JDBI?

我正在使用 Dropwizard 及其 JDBI module 来连接到 SQLite 数据库。我已经设置了数据库,以便它具有适当的 FK,但我仍然需要告诉 SQLite 启用它的约束。

我知道您可以配置它 using a properties instance,但我不知道如何将它与 Dropwizard 中的 JDBI 设置一起使用。

您需要将以下 属性 添加到您的 .yml 设置文件中:foreign_keys: true.

它应该看起来像这样:

database:
  # the name of your JDBC driver
  driverClass: org.sqlite.JDBC

  # the JDBC URL
  url: jdbc:sqlite:databasefile.db

  # any properties specific to your JDBC driver:
  properties:
    foreign_keys: true

之后你的 SQLite 数据库中应该有 FK 约束。您可以查看 SQLiteConfig class 了解更多属性。