Attaches a sort specification to an enum value used by an @orderBy argument. Exactly one of index, fields, or primaryKey must be set on each enum value; together they tell the generator which columns to emit in ORDER BY.

SDL signature

directive @order(
    index: String,
    fields: [FieldSort!],
    primaryKey: Boolean = false
) on ENUM_VALUE

input FieldSort {
    name:      String
    path:      [ReferenceElement!]
    method:    ExternalCodeReference
    collate:   String
    direction: SortDirection
}

Parameters

Name Type Default Description

index

String

none

Name of a database index. The generator looks up the index on the parent table at build time and emits its column list. Index code generation must be enabled in jOOQ (<includeIndexes>true</includeIndexes>).

fields

[FieldSort!]

none

One or more sort specifications. Each FieldSort sets exactly one of name (a column of the jOOQ table) and method (a static method returning the sort key), with an optional collate (database-specific collation name, e.g. "xdanish_ai") and direction. Beside name, an optional path resolves the column on a to-one related table instead; see Sorting by a related value.

primaryKey

Boolean

false

When true, sorts by the parent table’s primary key columns in declaration order. Useful for stable pagination cursors.

Canonical example

enum FilmSort {
    FILM_ID @order(primaryKey: true)
    TITLE   @order(fields: [{name: "title"}])
}

enum ActorSort {
    ACTOR_ID   @order(primaryKey: true)
    FIRST_NAME @order(fields: [{name: "first_name"}])
    LAST_NAME  @order(fields: [{name: "last_name"}])
}

FILM_ID @order(primaryKey: true) sorts by film.film_id, the primary key of the bound table. TITLE @order(fields: [{name: "title"}]) sorts by the title column without requiring a database index. On a connection, a value whose columns do not identify a row has the primary key appended for the cursor, so TITLE pages on title, film_id; see Stable cursors and tie-breakers. A column that admits NULL sorts its NULL rows last in either direction; see Where NULL sorts.

Index-based sorting (with optional multi-column index):

enum FilmOrderByField {
    LANGUAGE        @order(index: "IDX_FK_LANGUAGE_ID")
    TITLE           @order(index: "IDX_TITLE")
    STORE_ID_FILM_ID @order(index: "idx_store_id_film_id")
}

Field-based sorting with collation:

enum PersonOrderByField {
    NAME_NORSK @order(fields: [
        {name: "LAST_NAME",  collate: "xdanish_ai"},
        {name: "FIRST_NAME", collate: "xdanish_ai"}
    ])
    STATUS @order(fields: [{name: "ACTIVE"}])
}

path: sorts by a column on a table reached through to-one hops, in the element shape of @reference(path:); method: sorts by a key a static method derives, with @externalField's calling convention (the field’s table in, a Field<X> out):

enum FilmLanguageSort {
    ORIGINAL_LANGUAGE @order(fields: [{name: "name", path: [{key: "film_original_language_id_fkey"}]}])
    LATEST_STOCK      @order(fields: [{method: {className: "com.example.FilmSortKeys", method: "latestStockStore"}}])
}

ORIGINAL_LANGUAGE sorts films by the name of their original language, a film with none sorting last; LATEST_STOCK by a value the method computes from a film’s many inventory rows. On a connection neither counts toward identifying a row, so both page on the key and then film_id. See How-to: Sort by a value on another table for the to-one rule and the no-code alternative.

The enum’s parent type is determined by where the enum is referenced from (the @orderBy argument); column names resolve against that type’s @table.

Constraints

  • Exactly one of index, fields, or primaryKey must be set per enum value. Setting two, or none, fails the build.

  • index requires jOOQ index generation. With <includeIndexes>true</includeIndexes> disabled, the lookup throws at startup with a "no matching index" error.

  • fields[].name must be a real column on the bound table, or on the table its path lands on (SORT_KEY_COLUMN_OFF_PATH). Resolution mirrors @field: case-sensitive against the jOOQ-generated identifiers.

  • Each FieldSort sets exactly one of name and method, and path only beside name. Any other combination fails the build.

  • Every hop of a path must be to-one, proven from the jOOQ catalog: the hop’s join columns cover a primary or unique key of the table it reaches. A to-many hop (SORT_KEY_PATH_TO_MANY) or a hop with a condition: (SORT_KEY_PATH_CONDITION) fails the build and points at method, and so does a key that does not join the table the path stands on (SORT_KEY_PATH_UNRESOLVED).

  • A method names a public static method taking the field’s jOOQ table and returning a parameterised Field<X>. method is required inside the reference and argMapping is rejected.

  • path and method are not available on a multi-table interface or union, or over a table-valued function result.

  • collate is passed verbatim to the database; misspelled collations surface only at query time.

  • @order applies to enum values only. Use it on every value of an enum referenced by @orderBy; missing values fail the build.

  • @index is the deprecated single-purpose alias for @order(index:). New code should use @order.

See also

  • @orderBy is the entry point that activates @order-bearing enums.

  • @defaultOrder reuses the same index / fields / primaryKey shape on the field rather than the enum.

  • @index is deprecated; migrate to @order(index:).