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

Commit

Permalink
Add uniqueKeys property to Table Schema (#30)
Browse files Browse the repository at this point in the history
* Updated the spec

* Updated the profile

* Updated letter case

* Updated json formatting

* Updated the example

* Fixed profile

* Added `non-empty array`

* Improved wording
  • Loading branch information
roll authored Feb 20, 2024
1 parent 1214f3f commit d28f66c
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
41 changes: 41 additions & 0 deletions content/docs/specifications/table-schema.md
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,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

0 comments on commit d28f66c

Please sign in to comment.