You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 9493d1b
Browse filesBrowse the repository at this point in the historyBrowse files
* Test relationships with type foreignKey (#1691)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016DrmFBwhFWiWT72rvzuEi1
* Document the foreign key check
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016DrmFBwhFWiWT72rvzuEi1
* Give each relationship check a key that names its target
A column with two relationships, e.g. line_items.order_id referencing both
orders.order_id and invoices.order_id, no longer shares one check key: before,
the second check's result overwrote the first and the second had none. The key
now includes the referenced schema and columns, using physical names like the
other field checks: line_items__order_id__orders__order_id__field_relationship.
* Name the relationship check type field_relationships, as dbt sync does
dbt sync already reports ODCS relationships as field_relationships; the
datacontract test check now uses the same type and key suffix, so both report
the same type and dimension (consistency) for a foreign key.
* Simplify the relationship validity check and shorten its comments
* Test relationship checks with physicalName on both sides
* Name the cause when a relationship cannot be checked
The warning for an unresolvable relationship now says what is wrong instead of
listing every possibility: the contract has no schema X, schema X has no
property Y, the reference is not in schema.property form (e.g. a fully
qualified schema/<id>/properties/<id> reference), a composite key references
more than one schema, or from and to differ in length.
* Hint at --schema-name when the referenced model cannot be read
With --schema-name, a file server reads only the selected schema, so a
relationship to another schema ends as a warning. The warning now says so and
suggests testing without --schema-name.
* Warn when a relationship's from names another schema
Only the declaring schema's own name is stripped from from. Before, any prefix
was dropped, so a relationship declared on orders with from:
line_items.order_id checked orders.order_id against itself and always passed.
It is now a warning: orders has no property line_items.order_id.
---------
Co-authored-by: Jarich Braeckevelt <jarichb@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Jakob Schödl <jakob.schoedl@entropy-data.com>
Copy file name to clipboardExpand all lines: docs/docs/schema.md
+22Lines changed: 22 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -24,6 +24,7 @@ datacontract test --checks properties datacontract.yaml
24
24
|`required`| property | No missing values |
25
25
|`unique`| property | No duplicate values |
26
26
|`primaryKey`| property | No missing values **and** no duplicates |
27
+
|`relationships` (`type: foreignKey`) | property, schema | Every non-null key exists in the referenced schema |
27
28
|`logicalTypeOptions.minLength` / `maxLength`| property | Value length within bounds |
28
29
|`logicalTypeOptions.minimum` / `maximum`| property | Value within bounds (inclusive) |
29
30
|`logicalTypeOptions.exclusiveMinimum` / `exclusiveMaximum`| property | Value within bounds (exclusive) |
@@ -139,6 +140,27 @@ properties:
139
140
140
141
> Produces: `order_id` not null, `line_number` not null, and `(order_id, line_number)` unique.
141
142
143
+
## Foreign keys
144
+
145
+
A `relationships` entry of `type: foreignKey` checks that every non-null value of the referencing column exists in the referenced one. Declare it on the property, with `to`, or on the schema, with `from` and `to` lists for a composite key:
146
+
147
+
```yaml
148
+
schema:
149
+
- name: line_items
150
+
properties:
151
+
- name: order_id
152
+
relationships:
153
+
- type: foreignKey
154
+
to: orders.order_id
155
+
- name: shipments
156
+
relationships:
157
+
- type: foreignKey
158
+
from: [shipments.order_id, shipments.line_no]
159
+
to: [order_lines.order_id, order_lines.line_no]
160
+
```
161
+
162
+
The referenced schema must be read in the same run. When `--schema-name` selects only the referencing schema of a file server, the check is reported as a warning.
163
+
142
164
## Value constraints
143
165
144
166
`logicalTypeOptions`turns into value checks that behave identically on every backend — the CLI compiles each into dialect-specific SQL.
0 commit comments