Skip to content

Support custom ORDER BY for virtual fields added via AddField  #666

@YasinSamooei

Description

@YasinSamooei

In my project, I use info.AddField to add virtual (computed) columns to the list page that are not present in the database table — for example:

info.AddField("Groupe", "display_count", db.Varchar).
    FieldDisplay(func(model types.FieldModel) interface{} {
        cnt, _ := db.WithDriverAndConnection("default", condb).
            Table("map_message_groups").
            Where("message_id", "=", model.Row["uid"]).Count()
        return cnt
    }).
    FieldSortable()

The problem is:

  • FieldSortable() works only for real DB columns.
  • Virtual columns computed in FieldDisplay cannot currently be sorted, because there is no FieldOrderBy() (or similar) API for AddField to define a custom SQL expression for ordering.

I want to keep these virtual columns without altering the database schema, but still allow server‑side sorting via a correlated subquery or custom SQL expression.


Impact:

  • Common for dashboards to show counts or derived data without adding DB columns.
  • Without this capability, sorting requires either:
    • Fetching all rows and sorting in memory (slow, breaks pagination), or
    • Creating extra DB columns just for ordering (undesirable for many projects).

Suggested enhancement:

  • Add an API similar to ColumnOrderBy for AddField, e.g.:
.FieldOrderBy(func(order string, table string) string {
    return fmt.Sprintf("(SELECT COUNT(1) FROM map_message_groups mg WHERE mg.message_id = %s.uid) %s", table, order)
})

This would let developers provide a custom ORDER BY expression for non‑DB fields while keeping current pagination and efficiency.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions