Skip to content

[feat] Suggest related tables with foreign key relationships for JOIN and ON (#975)#1747

Merged
scottnemes merged 9 commits intodbcli:mainfrom
scottnemes:feat/975/suggest-fk-joins
Mar 27, 2026
Merged

[feat] Suggest related tables with foreign key relationships for JOIN and ON (#975)#1747
scottnemes merged 9 commits intodbcli:mainfrom
scottnemes:feat/975/suggest-fk-joins

Conversation

@scottnemes
Copy link
Copy Markdown
Contributor

Description

This PR adds foreign key relationship logic for completions:

  1. After JOIN, tables with foreign key relationships to the given table will appear first in the list
  2. After ON, the foreign key columns will be suggested to complete the JOIN

Example tables:
image

Note tables with foreign keys (orders and rosters) showing up first
image

Foreign key columns showing up after ON to join persons with orders
image

Same for rosters
image

Works with aliases as well
image

No foreign keys, just show the tables as expected
image

Resolves #975

Checklist

  • I added this contribution to the changelog.md file.
  • I added my name to the AUTHORS file (or it's already there).
  • To lint and format the code, I ran
    uv run ruff check && uv run ruff format && uv run mypy --install-types .

@scottnemes scottnemes self-assigned this Mar 27, 2026
@github-actions
Copy link
Copy Markdown

  1. Correctness regression: FK ON suggestions can produce invalid SQL for quoted/reserved column names
    In sqlcompleter.py, table names are escaped, but FK column names are stored raw and later interpolated directly in _fk_join_conditions.
    Impact: for FK columns like order, user-id, or names requiring backticks, completion returns invalid expressions (e.g. o.order = u.id instead of o.`order` = u.id).
    Action: escape col/ref_col when persisting FK metadata (or when building condition strings), consistent with extend_columns() behavior.

  2. Correctness edge case: JOIN prioritization can use FK metadata from the wrong schema
    In sqlcompleter.py, FK-priority lookup ignores parsed table schema and keys only by table name.
    Impact: for queries joining schema-qualified tables (e.g. other_db.orders), suggestions may be incorrectly prioritized using current-db FK metadata if table names overlap across DBs.
    Action: include schema in FK-priority matching, or explicitly skip FK-priority when parsed tables are from a non-current schema.

  3. Missing tests for the above edge cases
    New FK tests in test_smart_completion_public_schema_only.py cover happy paths but not identifier escaping or cross-schema JOINs.
    Action: add tests for:

    • FK columns requiring escaping/backticks in ON condition suggestions.
    • JOIN prioritization when query uses schema-qualified tables outside current DB.

No direct security issues stood out in this PR diff.

@scottnemes scottnemes requested a review from rolandwalker March 27, 2026 03:47
@rolandwalker
Copy link
Copy Markdown
Contributor

Legit! Going all the way with the full ON … = … is awesome.

In the case that the foreign keys exist, we should still allow completion on non-FK columns, just prioritize the FK case.

@scottnemes scottnemes merged commit 1f33072 into dbcli:main Mar 27, 2026
8 checks passed
@scottnemes scottnemes deleted the feat/975/suggest-fk-joins branch March 27, 2026 22:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Complete JOIN statements by using a foreign key

2 participants