# ❖ ForeignToPrimaryKeySubset

{% 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 **ForeignToPrimaryKeySubset** constraint when you have a 1-to-many connection between 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%2FNDP7hSnEP6enF0xY0cNc%2Fforeign-to-primary-key-subset_May%208%202025.png?alt=media&#x26;token=5ef39dc2-e6af-45f0-be1d-854402197bb5" alt=""><figcaption><p>In this example, only premium type accounts are allowed to have perks.</p></figcaption></figure>

## Constraint API

Create a `ForeignToPrimaryKeySubset`  constraint.

**Parameters**:&#x20;

* (required) `parent_table_name`: A string with the name of the parent table in the relationship
* (required) `child_table_name`: A string with the name of the child table in the relationship
* (required) `child_foreign_key`: A string with the name of the foreign key column in the child. This column is only allowed to be connected to the parent table with certain values.
* (required) `conditional_column_name`: A string with the name of the conditional column in the parent table. The values in this column determine which connections are allowed.
* (required) `conditional_values`: A list of values of the conditional column. Only these particular values are allowed to have connections with the child table.

```python
from sdv.cag import ForeignToPrimaryKeySubset

my_constraint = ForeignToPrimaryKeySubset(
    parent_table_name='Accounts',
    child_table_name='Perks',
    child_foreign_key='ID',
    conditional_column_name='Type',
    conditional_values=['PREMIUM'])
```

Make sure that all the tables and columns you provide are listed in your [Metadata](https://docs.sdv.dev/sdv/concepts/metadata), as well as the relationship between the primary and foreign 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).

## 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=['ForeignToPrimaryKeySubset']
)
```

**Detection Parameters**: None


---

# 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/foreigntoprimarykeysubset.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.
