# ❖ ForeignToForeignKey

{% 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 **ForeignToForeignKey** constraint when you have foreign keys in multiple tables but no primary key to attach them to. This may happen if your tables come from different domains and are linked together by the same concept.

<figure><img src="/files/hAD2NxZtt4vPKFczXnvz" alt=""><figcaption><p>In this example, the 'Products' table and the 'Shipments' table both have a column called 'Warehouse ID' that denotes the same concept (storage warehouse). Both these columns are foreign keys, and there is no primary key for Warehouse ID that is available to link them.</p></figcaption></figure>

## Constraint API

Create a `ForeignToForeignKey` constraint.

**Parameters**:&#x20;

* (required) `columns`: A list of dictionaries representing representing all the foreign key columns that are encoding the same concept. Each dictionary should have&#x20;
  * A `'table_name'` key mapping to the string name of the table, and
  * A `'foreign_key'` key mapping to the string name of the column. (If you have a composite key, provide a tuple of multiple strings.)
* `foreign_key_generation`: A string that describes whether the synthetic data for the foreign keys should contain brand new values, or reuse the ones that exist in your database
  * (default) `'new'`: Create new values in the synthetic data. These new values will be consistent everywhere in the database. *In our example above, the synthetic data would have brand-new Warehouse IDs representing new, synthetic warehouses. These warehouses would be consistent between the Products and Suppliers table.*
  * `'reuse'`: Reuse the values from the synthetic data. *In our example above, the synthetic data would have the same set of Warehouse IDs as the real data, within both the Products and Suppliers tables. These would represent the same warehouses.*

```python
from sdv.cag import ForeignToForeignKey

my_constraint = ForeignToForeignKey(
    columns=[{
        'table_name': 'Products',
        'foreign_key': 'Warehouse ID'
    },{
        'table_name': 'Shipments',
        'foreign_key': 'Warehouse ID'
    }],
    foreign_key_generation='new'
)
```

Make sure that all the tables and columns you provide are listed in your [Metadata](/sdv/concepts/metadata.md).

## 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 not available for this constraint. Please create the constraint object and add it to the synthesizer using the API described above.


---

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