❖ Polymorphic Relationship

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 page.

Use the PolymorphicRelationship constraint when the values inside a foreign key column can refer to different tables, depending on the context.

In this example, the Transaction Log table has a foreign key called Details. Sometimes, it refers to the Debit Accounts table and sometimes it refers to the Credit Accounts tables.

Constraint API

Create a PolymorphicRelationship constraint.

Parameters:

  • (required) table_name: A string with the name of the table that contains the foreign key column

  • (required) foreign_key_column_name: 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.

from sdv.cag import PolymorphicRelationship

my_constraint = PolymorphicRelationship(
    table_name='Transaction Log',
    foreign_key_column_name='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. 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.

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.

Last updated