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
Index columns change order when a functional expression comes after a column #1005
In an index that mixes columns and functional expressions, the expressions are moved in front of the columns, so the index gets a different column order from the one written.
Table users {
country varchar
name varchar
indexes {
(country, `lower(name)`)
}
}
exports to PostgreSQL as
CREATE INDEX ON "users" ((lower(name)), "country");
where ("country", (lower(name))) is expected. Column order matters for an index, since a query can only use its leading columns.
The cause is in packages/dbml-parse/src/core/global_modules/indexes/interpret.ts: it pushes every functional column first and then every plain column, from the two lists destructureIndexNode returns, instead of walking the elements in order.
Found while working on #172, reproduced on master at 9ffe17ac.
Thanks for filing this. Some extra data points in case they help with the fix.
Released versions. The reorder also happens in the published packages. I checked @dbml/core 8.3.1, 10.1.1 and 10.3.1 (each with the matching @dbml/parse, Node v26.8.1, 2026-10-17), and all three give the same result:
indexes entry
index.columns
PostgreSQL DDL
(a, `lower(b)`)
[`lower(b)`, a]
CREATE INDEX ON "t" ((lower(b)), "a");
(a, `lower(b)`, c)
[`lower(b)`, a, c]
CREATE INDEX ON "t" ((lower(b)), "a", "c");
(a, `lower(b)`, `upper(c)`)
[`lower(b)`, `upper(c)`, a]
CREATE INDEX ON "t" ((lower(b)), (upper(c)), "a");
For reference only: under Index Definition, the "(bonus) Composite index with expression" type is illustrated with the SQL CREATE INDEX ON users ( country, (lower(name)) ), where the column comes first. There is no matching DBML example, so this doesn't specify the DBML column order. The DBML examples that mix columns and expressions all put the expression first ((`id*3`,id)), so the docs never show this case.
A second reorder in the same interpreter. A fix that only changes the functional / non-functional loop may still leave this one in place. When index fields are written next to each other (juxtaposed), they can be parsed as a CallExpressionNode. The flatten step in indexes/interpret.ts (L81–L93 at v10.3.1) pushes argumentList before callee and walks from the outermost call inward. As a result, the fields come out in reverse source order. Output from @dbml/core 10.3.1:
"(a) (b)" => [["b","a"]] CREATE INDEX ON "t" ("b", "a");
"a (b)" => [["b","a"]] CREATE INDEX ON "t" ("b", "a");
"(a, b)(c)" => [["c","a","b"]] CREATE INDEX ON "t" ("c", "a", "b");
"(a, b) (c)" => [["c","a","b"]] CREATE INDEX ON "t" ("c", "a", "b");
a b and (a) b keep source order, because they are parsed as FunctionApplicationNode arguments rather than a call.
The code comment there says (id, name)(age, weight) is meant to be flattened, so this form seems intended. If so, it probably needs the same source-order fix. Whether juxtaposed fields should be accepted at all is a separate question, which I raised in #1008.
Repro script
// npm install --save-exact @dbml/[email protected] @dbml/[email protected] && node repro.cjsconst{ Parser, ModelExporter }=require('@dbml/core');for(constbodyof['(a, `lower(b)`)','(a) (b)','a (b)','(a, b)(c)']){constsrc=`Table t {\n a int\n b int\n c int\n indexes {\n ${body}\n }\n}\n`;constdb=newParser().parse(src,'dbmlv2');console.log(JSON.stringify(body),'=>',JSON.stringify(db.schemas[0].tables[0].indexes.map((ix)=>ix.columns.map((c)=>c.value))));console.log(' ',ModelExporter.export(db,'postgres',false).split('\n').filter((l)=>l.startsWith('CREATE INDEX')).join('\n '));}
Activity
kojifukada commentedon Oct 17, 2026
Thanks for filing this. Some extra data points in case they help with the fix.
Released versions. The reorder also happens in the published packages. I checked
@dbml/core8.3.1, 10.1.1 and 10.3.1 (each with the matching@dbml/parse, Node v26.8.1, 2026-10-17), and all three give the same result:indexesentryindex.columns(a, `lower(b)`)[`lower(b)`, a]CREATE INDEX ON "t" ((lower(b)), "a");(a, `lower(b)`, c)[`lower(b)`, a, c]CREATE INDEX ON "t" ((lower(b)), "a", "c");(a, `lower(b)`, `upper(c)`)[`lower(b)`, `upper(c)`, a]CREATE INDEX ON "t" ((lower(b)), (upper(c)), "a");For reference only: under Index Definition, the "(bonus) Composite index with expression" type is illustrated with the SQL
CREATE INDEX ON users ( country, (lower(name)) ), where the column comes first. There is no matching DBML example, so this doesn't specify the DBML column order. The DBML examples that mix columns and expressions all put the expression first ((`id*3`,id)), so the docs never show this case.A second reorder in the same interpreter. A fix that only changes the functional / non-functional loop may still leave this one in place. When index fields are written next to each other (juxtaposed), they can be parsed as a
CallExpressionNode. The flatten step inindexes/interpret.ts(L81–L93 at v10.3.1) pushesargumentListbeforecalleeand walks from the outermost call inward. As a result, the fields come out in reverse source order. Output from@dbml/core10.3.1:a band(a) bkeep source order, because they are parsed asFunctionApplicationNodearguments rather than a call.The code comment there says
(id, name)(age, weight)is meant to be flattened, so this form seems intended. If so, it probably needs the same source-order fix. Whether juxtaposed fields should be accepted at all is a separate question, which I raised in #1008.Repro script
fix(parse): keep the written order of index columns and expressions