# ❖ CompositeKeys

{% hint style="info" %}
❖ **SDV Enterprise Bundle**. This feature is available as part of the **CAG Bundle**, an optional add-on to SDV Enterprise. For more information, please visit the [CAG Bundle](/sdv/explore/sdv-bundles/cag.md) page.
{% endhint %}

Use the **CompositeKeys** constraint when multiple columns together form a primary key. Optionally, you may also have multi-column foreign key that connects to the primary key.

{% hint style="warning" %}
***This constraint is deprecated.*** Instead of supplying a CompositeKey constraint, supply composite key information in your metadata. For more information, see the [Metadata API](/sdv/concepts/metadata/metadata-api.md).

```json
{
    "tables": {
        "Patient visits": {
            "primary_key": ["Patient ID", "Date"],
            "columns": {
                "Patient ID": { "sdtype": "id", "regex_format": "P-[0-9]{5}" },
                "Date": { "sdtype": "datetime", "datetime_format": "%Y-%m-%d" },
                ...   
            }
        }
    },
    "relationships": [{
        "parent_table_name": "Patient visits",
        "parent_primary_key": ["Patient ID", "Date"],
        "child_table_name": "Lab results",
        "child_foreign_key": ["Patient ID", "Date"]
    }]
}
```

{% endhint %}

<figure><img src="/files/dndjuq8BFA6ux2bDxIAq" alt=""><figcaption><p>'Patient ID' and 'Date' together form the primary key of the table.</p></figcaption></figure>

## Constraint API

Create a `CompositeKeys` constraint that lists all the composite keys of your database (primary and foreign keys).

**Parameters**:&#x20;

* (required) `primary_keys`: A list of dictionaries that define the composite primary keys of all tables that have composite primary keys. Each dictionary should have the following keys:
  * `'table_name'`: A string with the name of the table
  * `'primary_key'`: A list of strings representing all the columns in the table that form the composite, primary key. At least 1 of these columns should be an sdtype id or PII column.
* `relationships`: A list of dictionaries that define the relationships from composite foreign keys to the composite primary keys defined above. Each dictionary should have the following keys:
  * `'parent_table_name'`: A string with the name of the parent table with the composite primary key (this should be listed in the `primary_keys` section above).
  * `'parent_primary_key'`: A list of columns that comprise the composite primary key of the table (this should be the same as the `primary_keys` section above)
  * `'child_table_name'`: A string with the name of the child table that refers to the parent
  * `'child_foreign_key'`: A list of columns that comprise the composite foreign key that refers to the parent. The length of this list should be the same as the primary key that it refers to.

```python
from sdv.cag import CompositeKeys

my_constraint = CompositeKeys(
    primary_keys=[{
        'table_name': 'Patient Visits',
        'primary_key': ['Patient ID', 'Date']
    }],
    relationships=[{
        'parent_table_name': 'Patient Visits',
        'parent_primary_key': ['Patient ID', 'Date'],
        'child_table_name': 'Test Results',
        'child_foreign_key': ['Patient ID', 'Visit Date']
    }]
)
```

Make sure that all the table and columns in you provide are in your [Metadata](/sdv/concepts/metadata.md). If you provide any foreign key connections, make sure they align with the columns in the primary key.

## Usage

Apply the constraint to any SDV synthesizer. Then fit and sample as usual.

```python
synthesizer = HSASynthesizer(metadata)
synthesizer.add_constraints([my_constraint])

synthesizer.fit(data)
synthetic_data = synthesizer.sample()
```

For more information about using predefined constraints, please see the [**Constraint-Augmented Generation tutorial**](https://colab.research.google.com/drive/1WCMQujfVKL5giULZXOPPIBoMfzR9Zj68?usp=sharing).


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.sdv.dev/sdv/concepts/constraint-augmented-generation-cag/predefined-constraints/compositekeys.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
