Skip to main content

generate:model

If you are looking to generate a resourceful (CRUD) controller with a model, see the resource generator documentation.

To generate an STI child model, see the sti-child generator documentation.

The generate:model CLI documentation follows below, but more details about generating a model are included in the model generator documentation.

Usage​

pnpm psy g:model [options] <modelName> [columnsWithTypes...]

Arguments​

  • <modelName>: The name of the model to create, e.g. Post or Settings/CommunicationPreferences.

  • [columnsWithTypes...]: Space separated snake-case (except for belongs_to model name) properties like this: title:citext subtitle:string body_markdown:text style:enum:post_styles:formal,informal User:belongs_to

    All properties default to not nullable; null can be allowed by appending :optional: subtitle:string:optional

    Supported types​

    • uuid, uuid[]: a column optimized for storing UUIDs
    • citext, citext[]: case insensitive text (indexes and queries are automatically case insensitive)
    • encrypted: encrypted text (used in conjunction with the @deco.Encrypted decorator)
    • string, string[]: varchar; allowed length defaults to 255, but may be customized, e.g.: subtitle:string:128 or subtitle:string:128:optional
    • text, text[]
    • date, date[]
    • datetime, datetime[]
    • time, time[]
    • timetz, timetz[]
    • integer, integer[]
    • decimal, decimal[]: precision,scale is required, e.g.: volume:decimal:3,2 or volume:decimal:3,2:optional
      • Leveraging arrays, add the "[]" suffix, e.g.: volume:decimal[]:3,2
    • enum, enum[]: include the enum name to automatically create the enum: type:enum:room_types:bathroom,kitchen,bedroom or type:enum:room_types:bathroom,kitchen,bedroom:optional
      • Omit the enum values to leverage an existing enum (omits the enum type creation): type:enum:room_types or type:enum:room_types:optional
      • Leveraging arrays, add the "[]" suffix, e.g.: type:enum[]:room_types:bathroom,kitchen,bedroom
    • belongs_to: not only adds a foreign key to the migration, but also adds a BelongsTo association to the generated model:
      • Include the fully qualified model name, e.g., if the Coach model is in src/app/models/Health/Coach: Health/Coach:belongs_to
      • Aliased FK shorthand — append @alias to generate a foreign key with a custom column name. This is the canonical pattern for _by columns (e.g. canceled_by_id) and for multiple foreign keys to the same model: InternalUser@canceled_by:belongs_to:optional produces a canceled_by_id column, a canceledById model property, and a canceledBy association — all from one token. Examples:
        • User@created_by:belongs_to — created_by_id FK, createdBy association
        • Message@last_inbound:belongs_to:optional — last_inbound_id FK, lastInbound association
        • Message@last_outbound:belongs_to:optional — last_outbound_id FK, lastOutbound association

The generator includes id, created_at, updated_at, deleted_at, and @SoftDelete() by default — do not specify these columns yourself. Use --no-soft-delete only when hard deletion is an intentional part of the model design.

Options​

  • --no-serializer: Do not generate a serializer for the model.
  • --no-soft-delete: Skip the default @SoftDelete() decorator and deleted_at column, so destroy() permanently removes rows instead of marking them removed.
  • --connection-name <connectionName>: The db connection you want this attached to (defaults to the default db connection) (default: "default").
  • --sti-base-serializer: Generate generically typed base serializers for the parent of an STI hierarchy (summary and default, plus each admin and internal pair requested). They accept the STI child class as a parameter, and when a type column is given, each summary serializer renders type right after id with an OpenAPI enum holding only the child's class name, which the default serializer inherits. Generate each child afterward with g:sti-child.
  • --table-name <tableName>: Explicit table name to use instead of the auto-generated one (useful when model namespaces produce long names).
  • --model-name <modelName>: Explicit model class name to use instead of the auto-generated one (e.g. --model-name=Kitchen for Room/Kitchen).
  • --admin-serializers: Generate admin serializer variants (AdminSerializer and AdminSummarySerializer) in addition to the default serializers.
  • --internal-serializers: Generate internal serializer variants (InternalSerializer and InternalSummarySerializer) in addition to the default serializers.
  • -h, --help: Display help for command.