No Foreign Keys. No Documentation. Good Luck Finding the Right JOIN.
Four practical checks for discovering database relationships before building your next dbt model. You inherit a database with hundreds of tables. Foreign keys are missing. Documentation is outdated. The person who desi
Four practical checks for discovering database relationships before building your next dbt model.
You inherit a database with hundreds of tables.
Foreign keys are missing. Documentation is outdated. The person who designed the schema left two years ago.
Your task sounds simple: build a new data model.
But before writing the SQL, you need to figure out how everything connects.
If you've ever worked with a legacy database, you probably know this feeling.
Here are four checks that can make the process a little less painful.
1. Stop Relying on Column Names
Consider these three columns:
orders.buyer_ref
customers.customer_id
accounts.account_id
Which one should you join to orders.buyer_ref?
Similar column names don't necessarily indicate a relationship. Different column names don't necessarily rule one out.
Start by examining data types, value formats, and existing model definitions.
Treat column names as clues, not evidence.
2. Check Whether the Values Actually Match
Once you've identified a candidate JOIN key, check how many distinct values exist in both columns.
WITH source_keys AS (
SELECT DISTINCT buyer_ref
FROM orders
WHERE buyer_ref IS NOT NULL
),
target_keys AS (
SELECT DISTINCT customer_id
FROM customers
WHERE customer_id IS NOT NULL
)
SELECT
COUNT(*) AS source_keys,
COUNT(t.customer_id) AS matched_keys,
ROUND(
100.0 * COUNT(t.customer_id)
/ NULLIF(COUNT(*), 0), 2
) AS inclusion_pct
FROM source_keys s
LEFT JOIN target_keys t
ON s.buyer_ref = t.customer_id;
A high inclusion rate provides evidence that the columns may be related.
A low inclusion rate deserves further investigation. It could indicate an incorrect key, missing records, or differences in how the two datasets are maintained.
Even 100% inclusion doesn't prove that two columns represent the same business entity.
3. Check Cardinality Before Writing the JOIN
Finding matching values is only half the job.
You also need to understand whether the relationship is one-to-one, many-to-one, or many-to-many.
Start by checking the candidate target key:
SELECT
customer_id,
COUNT(*) AS row_count
FROM customers
WHERE customer_id IS NOT NULL
GROUP BY customer_id
HAVING COUNT(*) > 1;
If this query returns records, the target key isn't unique.
Run a similar check on the source column to understand the observed cardinality.
Before using the relationship, make sure it fits the intended grain of your dbt model.
4. Turn Your Discovery into a dbt Test
Once you've validated the relationship, don't leave that knowledge buried inside a SQL file.
Document it and add a relationship test:
models:
- name: stg_orders
columns:
- name: buyer_ref
data_tests:
- relationships:
arguments:
to: ref('stg_customers')
field: customer_id
If customer_id is expected to be unique and non-null, add the corresponding tests to the customer model as well.
The YAML example uses newer dbt test syntax; older versions may require the arguments directly under relationships.
Now you have a reusable check instead of a one-time investigation.
Can We Automate the Repetitive Parts?
These checks are useful when you already have a few candidate columns.
But what happens when you're dealing with hundreds of tables and thousands of columns?
That's the problem we're working on at Arisyn.
We built IntaLink, a database relationship discovery platform that analyzes authorized database metadata and column-value evidence to identify candidate relationships, including those without explicit foreign keys.
We also developed IntaLink for dbt, a VS Code extension that brings discovered relationship information directly into the development workflow.
Instead of repeatedly investigating JOIN keys, developers can:
- Get suggestions for related tables and JOIN conditions.
- Inspect value inclusion, distinct-value statistics, and inferred cardinality.
- Flag potential JOIN risks.
- Reuse relationship information across supported dbt model transformations.
Relationship discovery takes place in IntaLink Cloud. The extension uses synchronized relationship sets to provide suggestions while you write SQL.
It doesn't replace the four checks above or your understanding of the business. It helps automate parts of the investigation and makes the resulting relationship knowledge easier to reuse.
Try It Before Installing Anything
We've created an interactive playground where you can explore relationship-aware JOIN suggestions directly in your browser using sample data.
No installation. No database connection.
Learn more:
The next time you inherit an unfamiliar database, try these four checks before writing your JOINs.
Better yet, make the relationships you discover reusable, so the next developer doesn't have to start from scratch.
Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.

