You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Feature request - Descending Fields in Indexing #172
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:
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.
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:
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.
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:
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.
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.
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. 🐱
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.
Activity
Roshan1299 commentedon Oct 13, 2026
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 (
IndexColumnstores a type and value), so there's no way to represent sort order. Using an expression as a workaround doesn't work either, sincecreated_at descgets exported as((created_at desc)), which isn't valid SQL.My proposed approach:
asc/descafter a column inside an index, following SQL conventions:DESCin the PostgreSQL, MySQL, SQL Server, Oracle, and DBML exporters, ensuring DBML round-trips correctly.I have two questions before proceeding:
DESCin this PR, or would you prefer that as a follow-up?If this approach sounds good, could you assign the issue to me? @huydo862003
huydo862003 commentedon Oct 13, 2026
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
DESCin this PR, but you can split the PRs if you'd like to.Roshan1299 commentedon Oct 14, 2026
Thanks @huydo862003! I ran a few spellings through
dbml-parseto find one that fits the parser:created_at descanddesc(created_at)are already valid DBML today: both are a composite index oncreated_atand a column nameddesc, so adesckeyword there would change their meaning.-created_atalready 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:The one catch: on a bare line,
country -created_atparses 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.huydo862003 commentedon Oct 14, 2026
@Roshan1299 Hi. Yes, it does seems like a nice syntax. Once done, you can submit a PR and I'll review for you 🐱
Roshan1299 commentedon Oct 15, 2026
Hi @huydo862003, I hit one problem with
-while building it. On any line of anIndexesblock but the first,-created_atis read as a subtraction from the line above, even across a blank line, because an infix operator continues an expression over a newline: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:
-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.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
masteralong 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 inpackages/dbml-parse/src/core/global_modules/indexes/interpret.tsadds expression columns and plain columns in two separate passes.Recordsrow that starts with a negative number fails to parse unless it's the first row, for the same reason as above.huydo862003 commentedon Oct 16, 2026
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. 🐱
Roshan1299 commentedon Oct 16, 2026
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-colsyntax here once it's in.Roshan1299 commentedon Oct 22, 2026
Hi @huydo862003 , I've opened a PR here: #1018 - would appreciate a review when you get a chance!