Skip to content

feat(schema): expose foreign_key_type_field on polymorphic relation#316

Merged
christophebrun-forest merged 1 commit into
mainfrom
fix/polymorphic-foreign-key-type-field
Jun 10, 2026
Merged

feat(schema): expose foreign_key_type_field on polymorphic relation#316
christophebrun-forest merged 1 commit into
mainfrom
fix/polymorphic-foreign-key-type-field

Conversation

@christophebrun-forest

@christophebrun-forest christophebrun-forest commented Jun 10, 2026

Copy link
Copy Markdown
Member

Context

In the schema emitted for PolymorphicManyToOne relations (belongs_to ..., polymorphic: true), we already exposed the list of target models via polymorphic_referenced_models, but not the name of the discriminator column (the _type, e.g. addressable_type). As a result, the Forest query executor could not resolve which model a polymorphic record targets.

Change

On the polymorphic relation field of the emitted schema, expose the discriminator column name as metadata, next to polymorphic_referenced_models:

foreign_key_type_field: relation.foreign_key_type_field  # e.g. "addressable_type"
  • Naming convention follows the neighbouring polymorphic key (polymorphic_referenced_models): snake_case foreign_key_type_field.
  • No allowed-keys whitelist exists in agent-ruby (SchemaEmitter serializes the collection hash as-is), so nothing to add there.
  • The raw _id/_type columns remain hidden from the frontend — only the name of the _type column is exposed as relation metadata; behaviour unchanged.

Tests

  • Updated generator_field_polymorphic_spec.rb to assert foreign_key_type_field: 'addressable_type'.
  • Targeted suite: 3 examples, 0 failures. Full utils/schema/ suite: 199 examples, 0 failures.

Ref

🤖 Generated with Claude Code

Note

Expose foreign_key_type_field in polymorphic relation field schema

Adds foreign_key_type_field to the hash returned by GeneratorField.build_column_schema for polymorphic foreign key columns, populated from relation.foreign_key_type_field. The spec in generator_field_polymorphic_spec.rb is updated to assert the new field is present with the correct value.

Macroscope summarized 2d13057.

Expose the discriminator column name (e.g. "addressable_type") as
metadata of the PolymorphicManyToOne relation in the emitted schema,
alongside polymorphic_referenced_models, so the Forest query executor
can resolve which model a polymorphic record targets.

The raw _id/_type columns remain hidden from the frontend; only the
NAME of the _type column is exposed as relation metadata.

Ref: PRD-493

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

@matthv matthv left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@christophebrun-forest christophebrun-forest merged commit 157af7e into main Jun 10, 2026
44 checks passed
@christophebrun-forest christophebrun-forest deleted the fix/polymorphic-foreign-key-type-field branch June 10, 2026 14:06
forest-bot added a commit that referenced this pull request Jun 10, 2026
# [1.32.0](v1.31.0...v1.32.0) (2026-06-10)

### Features

* **schema:** expose foreign_key_type_field on polymorphic relation ([#316](#316)) ([157af7e](157af7e))
@forest-bot

Copy link
Copy Markdown
Member

🎉 This PR is included in version 1.32.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants