compileForeignKeys method

  1. @override
String? compileForeignKeys(
  1. String table, {
  2. String? schema,
})
override

Compiles SQL to list foreign keys for a table within an optional schema.

Implementation

@override
String? compileForeignKeys(String table, {String? schema}) {
  final filter = schema == null ? '' : ' AND tc.table_schema = ?';
  return 'SELECT tc.constraint_name, tc.table_schema, '
      'kcu.column_name, ccu.table_schema AS referenced_schema, '
      'ccu.table_name AS referenced_table, '
      'ccu.column_name AS referenced_column, '
      'rc.update_rule, rc.delete_rule, kcu.ordinal_position '
      'FROM information_schema.table_constraints tc '
      'JOIN information_schema.key_column_usage kcu '
      '  ON kcu.constraint_name = tc.constraint_name '
      ' AND kcu.constraint_schema = tc.constraint_schema '
      'JOIN information_schema.constraint_column_usage ccu '
      '  ON ccu.constraint_name = tc.constraint_name '
      ' AND ccu.constraint_schema = tc.constraint_schema '
      'JOIN information_schema.referential_constraints rc '
      '  ON rc.constraint_name = tc.constraint_name '
      ' AND rc.constraint_schema = tc.constraint_schema '
      "WHERE tc.constraint_type = 'FOREIGN KEY' "
      '  AND tc.table_name = ?'
      '$filter '
      'ORDER BY tc.constraint_name, kcu.ordinal_position';
}