get_table_relationships
Identify foreign key relationships for a specified table in PostgreSQL, enabling users to understand and map database schema dependencies efficiently.
Instructions
Get foreign key relationships for a table
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| schema | No | Schema name (default: public) | public |
| table_name | Yes | Name of the table |
Implementation Reference
- src/index.js:261-308 (handler)The main handler function implementing the get_table_relationships tool logic. It queries PostgreSQL's information_schema for foreign key relationships of the given table.async getTableRelationships(tableName, schema = 'public') { const client = await this.connectToDatabase(); try { const query = ` SELECT kcu.column_name, ccu.table_schema AS foreign_table_schema, ccu.table_name AS foreign_table_name, ccu.column_name AS foreign_column_name, rc.constraint_name, rc.update_rule, rc.delete_rule FROM information_schema.table_constraints AS tc JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name AND tc.table_schema = kcu.table_schema JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name AND ccu.table_schema = tc.table_schema JOIN information_schema.referential_constraints AS rc ON tc.constraint_name = rc.constraint_name WHERE tc.constraint_type = 'FOREIGN KEY' AND tc.table_schema = $1 AND tc.table_name = $2; `; const result = await client.query(query, [schema, tableName]); return { content: [ { type: 'text', text: result.rows.length > 0 ? `Foreign key relationships for "${schema}.${tableName}":\n\n` + result.rows.map(row => `${row.column_name} → ${row.foreign_table_schema}.${row.foreign_table_name}.${row.foreign_column_name}\n` + ` Constraint: ${row.constraint_name}\n` + ` On Update: ${row.update_rule}, On Delete: ${row.delete_rule}` ).join('\n\n') : `No foreign key relationships found for table "${schema}.${tableName}"` }, ], }; } finally { await client.end(); } }
- src/index.js:93-107 (schema)The input schema definition for the get_table_relationships tool, specifying parameters table_name (required) and schema (optional).inputSchema: { type: 'object', properties: { table_name: { type: 'string', description: 'Name of the table', }, schema: { type: 'string', description: 'Schema name (default: public)', default: 'public' } }, required: ['table_name'], },
- src/index.js:90-108 (registration)Tool registration in the listTools response, including name, description, and input schema.{ name: 'get_table_relationships', description: 'Get foreign key relationships for a table', inputSchema: { type: 'object', properties: { table_name: { type: 'string', description: 'Name of the table', }, schema: { type: 'string', description: 'Schema name (default: public)', default: 'public' } }, required: ['table_name'], }, },
- src/index.js:151-152 (registration)Dispatcher case in the CallToolRequestHandler that maps the tool name to its handler function.case 'get_table_relationships': return await this.getTableRelationships(args.table_name, args?.schema || 'public');