Skip to content

Foreign Keys

Android SQLite

You can enable foreign key constraints for the Android SQLite driver through the driver's onOpen callback.

AndroidSqliteDriver(
  schema = Database.Schema,
  callback = object : AndroidSqliteDriver.Callback(Database.Schema) {
    override fun onOpen(db: SupportSQLiteDatabase) {
      db.setForeignKeyConstraintsEnabled(true)
    }
  }
)

JVM SQLite

You can enable foreign key constraints for the JVM SQLite driver by passing the setting to the driver's properties.

JdbcSqliteDriver(
  url = "...", 
  properties = Properties().apply { put("foreign_keys", "true") }
)

Native SQLite

You can enable foreign key constraints for the Native SQLite driver by enabling them in the database configuration.

NativeSqliteDriver(
  schema = Database.Schema,
  onConfiguration = { config: DatabaseConfiguration ->
    config.copy(
      extendedConfig = DatabaseConfiguration.Extended(foreignKeyConstraints = true)
    )
  }
)