Skip to main content

STI Child Model Generator

The STI (Single Table Inheritance) child generator creates new Dream models that extend another Dream model, enabling you to leverage single table inheritance patterns. This generator creates complete models, just like the regular model generator, but with inheritance relationships.

Prerequisites: Creating the STI Parent

Before generating STI children, you must first create the parent model, and it is recommended to use the --sti-base-serializer flag when doing so:

yarn psy g:resource --sti-base-serializer --owning-model=Place \
v1/host/places/rooms Room type:enum:room_types:Bathroom,Bedroom,Kitchen,Den,LivingRoom \
Place:belongs_to position:integer:optional deleted_at:datetime:optional

Why --sti-base-serializer? This flag creates the base serializer in a format that child serializers can properly extend, ensuring the inheritance hierarchy works correctly.

You can also create an STI parent using the model generator:

yarn psy g:model --sti-base-serializer \
Room type:enum:room_types:Bathroom,Bedroom,Kitchen,Den,LivingRoom \
Place:belongs_to position:integer:optional deleted_at:datetime:optional

Usage

yarn psy g:sti-child <childModelName> extends <parentModelName> [columnsWithTypes...]

Arguments:

  • <childModelName>: The name of the child model to create (e.g., AdminUser or Premium/Account)
  • extends: The literal word "extends" (required)
  • <parentModelName>: Fully qualified name of the parent model (e.g., User or Health/Coach)
  • [columnsWithTypes...]: Additional properties specific to the child model

Options:

  • --no-serializer: Skip generating a serializer for the child model

STI Example

This example creates an STI model for various rooms.

STI Base

Create the base Room model using the resource generator with the --sti-base-serializer flag:

yarn psy g:resource --sti-base-serializer --owning-model=Place \
v1/host/places/rooms Room type:enum:room_types:Bathroom,Bedroom,Kitchen,Den,LivingRoom \
Place:belongs_to position:integer:optional deleted_at:datetime:optional

STI Child

Then create an STI child:

yarn psy g:sti-child Room/Kitchen extends Room appliances:enum\[\]:appliance_types:stove,oven,microwave,dishwasher

Supported Column Types

STI child models support the same column types as the model generator.