> For the complete documentation index, see [llms.txt](https://docs.sdv.dev/sdv/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.sdv.dev/sdv/modeling/constraint-augmented-generation-cag/predefined-constraints/polymorphic-relationship.md).

# ❖ Polymorphic Relationship

{% 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 **PolymorphicRelationship** constraint when the values inside a foreign key column can refer to different tables, depending on the context.

<figure><img src="https://1967107441-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FfNxEeZzl9uFiJ4Zf4BRZ%2Fuploads%2Fvaabe7cSoun7wpK5u3KP%2Fcag-bundle-predefined-constraints-polymorphic-relationship_Nov%2024%202025.png?alt=media&amp;token=803db8d4-4bba-4467-ad8c-ace131924489" alt=""><figcaption><p>In this example, the <code>Transaction Log</code> table has a foreign key called <code>Details</code>. Sometimes, it refers to the <code>Debit Accounts</code> table and sometimes it refers to the <code>Credit Accounts</code> tables.</p></figcaption></figure>

## Constraint API

Create a `PolymorphicRelationship`  constraint.

**Parameters**:&#x20;

* (required) `table_name`: A string with the name of the table that contains the foreign key column
* (required) `foreign_key`: A string with the name of the foreign key column inside the table
* (required) `parent_table_names`: A list of strings containing the possible different parent tables that the foreign key column could be referring to
* `type_column_name`: Sometimes, the table may have a categorical column that controls which tables the foreign key refers to. In this case, provide a string with the name of this column.
* `type_value_to_table`: If there is a type column, provide a dictionary that maps each categorical value of the column to the table it refers to.

```python
from sdv.cag import PolymorphicRelationship

my_constraint = PolymorphicRelationship(
    table_name='Transaction Log',
    foreign_key='Details',
    parent_table_names=['Debit Accounts', 'Credit Accounts'],
    type_column_name='Type',
    type_value_to_table={
        'CREDIT': 'Credit Accounts',
        'DEBIT': 'Debit Accounts'
    }
)
```

Make sure that all the tables and columns you provide are listed in your [Metadata](/sdv/integration/metadata.md). The table relationships will not be part of the metadata, as a polymorphic relationship cannot be inputted into the metadata.

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

**Detection Parameters**: None


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.sdv.dev/sdv/modeling/constraint-augmented-generation-cag/predefined-constraints/polymorphic-relationship.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
