Skip to content
This repository has been archived by the owner on Oct 28, 2024. It is now read-only.

Add uniqueKeys property to Table Schema #30

Merged
merged 8 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions content/docs/specifications/table-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,47 @@ Here's an example with an array primary key:
"primaryKey": ["a", "c"]
}

### Unique Keys

A unique key is a field or a set of fields that are required to have unique logical values in each row in the table. It is directly modeled on the concept of unique constraint in SQL.

The `uniqueKeys` property, if present, `MUST` be a non-empty array. Each entry in the array `MUST` be a `uniqueKey`. A `uniqueKey` `MUST` be an array of strings with each string corresponding to one of the field `name` values in the `fields` array, denoting that the unique key is made up of those fields. It is acceptable to have an array with a single value, indicating just one field in the unique key.

An example of using the `uniqueKeys` property:

```json
"fields": [
{
"name": "a"
},
{
"name": "b"
},
{
"name": "c"
}
],
"uniqueKeys": [
["a"],
["a", "b"],
["a", "c"]
]
```

In the case of the definition above, the data in the table has to be considered valid only if:

- each row has a unique logical value in the field `a`
- each row has a unique set of logical values in the fields `a` and `b`
- each row has a unique set of logical values in the fields `a` and `c`

#### Handling `null` values

All the field values that are on the logical level are considered to be `null` values `MUST` be excluded from the uniqueness check, as the `uniqueKeys` property is modeled on the concept of unique constraint in SQL.

#### Relation to `constraints.unique`

In contrast with `field.constraints.unique`, `uniqueKeys` allows to define uniqueness as a combination of fields. Both properties `SHOULD` be assessed separately.

### Foreign Keys

A foreign key is a reference where values in a field (or fields) on the
Expand Down
12 changes: 12 additions & 0 deletions profiles/dictionary/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ tableSchema:
}
primaryKey:
"$ref": "#/definitions/tableSchemaPrimaryKey"
uniqueKeys:
"$ref": "#/definitions/tableSchemaUniqueKeys"
foreignKeys:
type: array
minItems: 1
Expand Down Expand Up @@ -141,6 +143,16 @@ tableSchemaPrimaryKey:
"last_name"
]
}
tableSchemaUniqueKeys:
type: array
minItems: 1
uniqueItems: true
items:
type: array
minItems: 1
uniqueItems: true
items:
type: string
tableSchemaForeignKey:
title: Table Schema Foreign Key
description: Table Schema Foreign Key
Expand Down