diff --git a/mostlyai/sdk/domain.py b/mostlyai/sdk/domain.py index f01b787a..20249198 100644 --- a/mostlyai/sdk/domain.py +++ b/mostlyai/sdk/domain.py @@ -2680,11 +2680,9 @@ def add_model_configuration(cls, values): else: has_tabular_model = True has_language_model = False - if values.foreign_keys: - # Always train tabular model for linked tables to model sequences - for fk in values.foreign_keys: - if fk.is_context: - has_tabular_model = True + # Always train tabular model for tables with a primary key or linked tables to model sequences + if values.primary_key or (values.foreign_keys and any(fk.is_context for fk in values.foreign_keys)): + has_tabular_model = True # Remove model configurations that are not applicable for the model type if values.tabular_model_configuration and not has_tabular_model: values.tabular_model_configuration = None diff --git a/tests/test_domain.py b/tests/test_domain.py index 875176b0..f4704432 100644 --- a/tests/test_domain.py +++ b/tests/test_domain.py @@ -82,6 +82,19 @@ def assert_model_configuration(s: SourceTableConfig, has_tabular_model: bool, ha s = SourceTableConfig(**{"name": "tbl1", "primary_key": "id", "columns": [{"name": "id"}]}) assert_model_configuration(s, has_tabular_model=True, has_language_model=False) + # PK + language column (id: pk, name: lang_text) + s = SourceTableConfig( + **{ + "name": "tbl1", + "primary_key": "id", + "columns": [ + {"name": "id", "model_encoding_type": ModelEncodingType.tabular_categorical}, + {"name": "name", "model_encoding_type": ModelEncodingType.language_text}, + ], + } + ) + assert_model_configuration(s, has_tabular_model=True, has_language_model=True) + # PK + FK columns s = SourceTableConfig( **{ diff --git a/tools/custom_template/pydantic_v2/BaseModel.jinja2 b/tools/custom_template/pydantic_v2/BaseModel.jinja2 index 02f1cde4..65310c32 100644 --- a/tools/custom_template/pydantic_v2/BaseModel.jinja2 +++ b/tools/custom_template/pydantic_v2/BaseModel.jinja2 @@ -731,11 +731,9 @@ class {{ class_name }}({{ base_class }}):{% if comment is defined %} # {{ comme else: has_tabular_model = True has_language_model = False - if values.foreign_keys: - # Always train tabular model for linked tables to model sequences - for fk in values.foreign_keys: - if fk.is_context: - has_tabular_model = True + # Always train tabular model for tables with a primary key or linked tables to model sequences + if values.primary_key or (values.foreign_keys and any(fk.is_context for fk in values.foreign_keys)): + has_tabular_model = True # Remove model configurations that are not applicable for the model type if values.tabular_model_configuration and not has_tabular_model: values.tabular_model_configuration = None diff --git a/tools/model.py b/tools/model.py index 9edfd35f..72757f55 100644 --- a/tools/model.py +++ b/tools/model.py @@ -545,11 +545,9 @@ def add_model_configuration(cls, values): else: has_tabular_model = True has_language_model = False - if values.foreign_keys: - # Always train tabular model for linked tables to model sequences - for fk in values.foreign_keys: - if fk.is_context: - has_tabular_model = True + # Always train tabular model for tables with a primary key or linked tables to model sequences + if values.primary_key or (values.foreign_keys and any(fk.is_context for fk in values.foreign_keys)): + has_tabular_model = True # Remove model configurations that are not applicable for the model type if values.tabular_model_configuration and not has_tabular_model: values.tabular_model_configuration = None