Skip to content

Index columns change order when a functional expression comes after a column #1005

Closed

Description

@Roshan1299

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.

Activity

  1. kojifukada commented on Oct 17, 2026

    @kojifukada

    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.cjs
    const { Parser, ModelExporter } = require('@dbml/core');
    for (const body of ['(a, `lower(b)`)', '(a) (b)', 'a (b)', '(a, b)(c)']) {
      const src = `Table t {\n  a int\n  b int\n  c int\n  indexes {\n    ${body}\n  }\n}\n`;
      const db = new Parser().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    '));
    }
  2. added a commit that references this issue on Oct 19, 2026
    8a3ac8c
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions