Skip to content

Feature request - Descending Fields in Indexing #172

Closed

Description

@Stoods-hd

There doesn't seem to be a way of defining in an index has a "descending" value
(i.e. Ordering by the latest dates first)

Activity

  1. added
    enhancement
    help wanted
    good first issue
    on Nov 15, 2025
  2. Roshan1299 commented on Oct 13, 2026

    @Roshan1299
    Contributor

    Hi! I'd like to work on this issue. It was opened 4 years ago and hasn't received a response yet, so I'd like to confirm the approach before getting started.

    Currently, an index column can only store a name or an expression (IndexColumn stores a type and value), so there's no way to represent sort order. Using an expression as a workaround doesn't work either, since created_at desc gets exported as ((created_at desc)), which isn't valid SQL.

    My proposed approach:

    • Syntax: Support optional asc/desc after a column inside an index, following SQL conventions:
      indexes {
        (created_at desc, country)
        created_at desc [name: 'created_at_index']
      }
      
    • Model: Add an optional sort order to index columns and include it in JSON exports.
    • Exporters: Support DESC in the PostgreSQL, MySQL, SQL Server, Oracle, and DBML exporters, ensuring DBML round-trips correctly.
    • Documentation and tests: Add coverage for all supported changes.

    I have two questions before proceeding:

    1. Does this syntax look good, or would you prefer an alternative, such as a per-column setting?
    2. Should SQL importers also preserve DESC in this PR, or would you prefer that as a follow-up?

    If this approach sounds good, could you assign the issue to me? @huydo862003

  3. huydo862003 commented on Oct 13, 2026

    @huydo862003
    Contributor

    Hi @Roshan1299:

    • The approach is generally good, the syntax looks ok but it's not currently compatible with the syntax the parser supports (the parser currently mostly supports normal expressions you find in most programming languages). -> If there is a more compatible syntax, it would be great

    • SQL importers should preferably preserve DESC in this PR, but you can split the PRs if you'd like to.

  4. Roshan1299 commented on Oct 14, 2026

    @Roshan1299
    Contributor

    Thanks @huydo862003! I ran a few spellings through dbml-parse to find one that fits the parser:

    • created_at desc and desc(created_at) are already valid DBML today: both are a composite index on created_at and a column named desc, so a desc keyword there would change their meaning.
    • -created_at already parses as a prefix expression and is only rejected by the index validator ("An index field must be an identifier, ..."), so allowing it changes nothing that's valid today. It also works inside a tuple, on a quoted name and on a functional expression:
    indexes {
      -created_at
      (-created_at, country)
      -"created at"
      -`lower(name)`
    }
    

    The one catch: on a bare line, country -created_at parses as a subtraction, so a mixed order needs the tuple form, (country, -created_at). ASC stays the default, so there's no + form to add.

    Would - for DESC work for you? If so, I'll do the parser, model, exporters, docs and the SQL importers in one PR as you suggested, and split the importers out if it gets too large.

  5. huydo862003 commented on Oct 14, 2026

    @huydo862003
    Contributor

    @Roshan1299 Hi. Yes, it does seems like a nice syntax. Once done, you can submit a PR and I'll review for you 🐱

  6. Roshan1299 commented on Oct 15, 2026

    @Roshan1299
    Contributor

    Hi @huydo862003, I hit one problem with - while building it. On any line of an Indexes block but the first, -created_at is read as a subtraction from the line above, even across a blank line, because an infix operator continues an expression over a newline:

    indexes {
      id
      -created_at    // parsed as `id - created_at`, "An index field must be an identifier, ..."
    }
    

    Wrapped in parentheses it works on any line: (-created_at) and (country, -created_at) each give their own index with the sort order. I missed this in my earlier comment, sorry.

    I tried stopping an infix operator at the start of a line the way the parser already stops (, but that breaks 10 existing parser tests (fuzz tests on refs among them), so I don't think it's the right fix for this PR.

    Two ways I can see:

    1. Keep - and write a descending column in parentheses, (-created_at). The DBML exporter would always write it that way, the docs would show that form, and the validator would point at the parentheses when it sees - at the start of a line instead of the generic message.
    2. A different syntax that doesn't depend on line breaks, if you have one in mind.

    I'd go with 1 unless you prefer otherwise. The rest (model, the four SQL exporters, and the PostgreSQL, MySQL, SQL Server and Oracle importers) is done and doesn't depend on this choice.

    Two things I noticed on master along the way, which I've left out of this PR. Happy to open issues for them if they're useful:

    • (country, `lower(name)`) stores the expression column first, so the index's column order changes. The interpreter in packages/dbml-parse/src/core/global_modules/indexes/interpret.ts adds expression columns and plain columns in two separate passes.
    • A Records row that starts with a negative number fails to parse unless it's the first row, for the same reason as above.
  7. huydo862003 commented on Oct 16, 2026

    @huydo862003
    Contributor

    Hi @Roshan1299, great finds. You can open those 2 separate issues.

    I believe the former issue can be fixed quite easily.

    The second issue, yeah we can consider disallowing expressions to span across newlines in the element body context if they are fully complete on their own line (note the exception for [], ()). I'm not sure if this can be easily fixed or not, but you're welcome to take a shot 😄

    I think once the second issue is resolved, you can implement the chosen DESC syntax. 🐱

  8. Roshan1299 commented on Oct 16, 2026

    @Roshan1299
    Contributor

    Thanks @huydo862003, opened #1005 for the index column order and #1006 for the line continuation. I'll take a shot at #1006 , keeping the change to element bodies with [] and () excepted as you suggested, and come back to the -col syntax here once it's in.

  9. Roshan1299 commented on Oct 22, 2026

    @Roshan1299
    Contributor

    Hi @huydo862003 , I've opened a PR here: #1018 - would appreciate a review when you get a chance!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementgood first issuehelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions