# ❖ PrimaryToPrimaryKeySubset

{% 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](https://docs.sdv.dev/sdv/explore/sdv-bundles/cag) page.
{% endhint %}

Use the **PrimaryToPrimaryKeySubset** constraint when you have a 1-to-1 connection between the primary keys of two or more tables but only certain values are allowed to have connections.

<figure><img src="https://1967107441-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfNxEeZzl9uFiJ4Zf4BRZ%2Fuploads%2F6P9OJvn5WwalXrvSDQPO%2Fprimary-to-primary-key-subset_May%208%202025.png?alt=media&#x26;token=45bb657c-0bcf-416f-938c-499996d9de40" alt=""><figcaption><p>In this example, only minors are supposed to have parental info</p></figcaption></figure>

## Constraint API

{% hint style="warning" %}
**This functionality is in Beta.** At this time, select SDV Enterprise users have been invited to use this feature.
{% endhint %}

Create a `PrimaryToPrimaryKeySubset` constraint.

**Parameters**:&#x20;

* (required) `main_table_name`: A string with the name of the main table that contains all the possible rows. Only some of these rows are allowed to be connected to other tables.
* (required) `conditional_column_name`: A string with the name of a column in the main table. The values of this column controls whether connections are allowed with other tables.
* (required) `relationships`: A dictionary that maps the name of each connected table with the conditional value that is allowed for the connection.

```python
from sdv.cag import PrimaryToPrimaryKeySubset

my_constraint = PrimaryToPrimaryKeySubset(
    main_table_name='Users',
    conditional_column_name='Is Minor?',
    relationships={
        'Parental Info': [True]
    })
```

Make sure that all the table and columns in you provide are in your [Metadata](https://docs.sdv.dev/sdv/concepts/metadata), and have a primary key associated with them.

## 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).

## Auto-Detection

Auto-detection is allowed for this constraint but it is not enabled by default.

If you'd like to auto-detect this constraint specifically, you can supply it in the auto-detection parameters. This will detect all instances of the constraint throughout the dataset.

```python
constraints = synthesizer.detect_constraints(
    data,
    constraint_classes=['PrimaryToPrimaryKeySubset']
)
```

**Detection Parameters**: None
