listForeignKeys method

  1. @override
Future<List<SchemaForeignKey>> listForeignKeys(
  1. String table, {
  2. String? schema,
})
inherited

Lists foreign keys for table within an optional schema.

Implementation

@override
Future<List<SchemaForeignKey>> listForeignKeys(
  String table, {
  String? schema,
}) async {
  final sql = _schemaCompiler.dialect.compileForeignKeys(
    table,
    schema: schema,
  );
  if (sql == null) {
    throw UnsupportedError('$driverName should support foreign key listing.');
  }

  final rows = await queryRaw(
    sql,
    _profile.schemaQueryParameters('foreignKeys', table, schema),
  );
  if (rows.any((row) => row['constraint_name'] != null)) {
    return _postgresForeignKeys(rows, table, schema: schema);
  }
  var counter = 0;
  return rows
      .map((row) {
        counter += 1;
        final columns = _splitColumns(row['columns']);
        final foreignColumns = _splitColumns(row['foreign_columns']);
        final foreignSchema = row['foreign_schema'] as String?;
        return SchemaForeignKey(
          name: 'fk_${table}_$counter',
          columns: columns,
          tableName: table,
          referencedTable: row['foreign_table'] as String,
          referencedColumns: foreignColumns,
          schema: _exposedSchema(_schemaOrDefault(schema)),
          referencedSchema: _exposedSchema(foreignSchema),
          onUpdate: row['on_update'] as String?,
          onDelete: row['on_delete'] as String?,
        );
      })
      .toList(growable: false);
}