Repository navigation
Description
Summary
In an indexes block, the docs only show two kinds of entry: a single field (booking_date) and a parenthesized, comma-separated tuple ((country, booking_date)). The parser also accepts several fields on one line separated by spaces or tabs, and turns them into one composite index. This grammar is not described anywhere in the docs, so a reader can't tell whether a b means one index or two.
Could you clarify the intended grammar of an index entry, and document it?
Environment
@dbml/core10.3.1 /@dbml/parse10.3.1 (the latest stable releases at the time of checking), Node v26.8.1. Checked on 2026-10-17.- The same results with
@dbml/core8.3.1 and 10.1.1. - At the time of checking,
packages/dbml-parse/src/core/parser/parser.tsand.../global_modules/indexes/interpret.tsonmasterwere the same files as in v10.3.1.
Repro
// npm install --save-exact @dbml/[email protected] @dbml/[email protected] && node repro.cjs
const { Parser, ModelExporter } = require('@dbml/core');
const bodies = [
'a b',
'(a) (b)',
'a (b)',
'(a, b) (c)',
'(a, b)(c)',
'(a)\n (b)',
'(a, b)',
];
for (const body of bodies) {
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');
const cols = db.schemas[0].tables[0].indexes.map((ix) => ix.columns.map((c) => c.value));
const ddl = ModelExporter.export(db, 'postgres', false)
.split('\n').filter((l) => l.startsWith('CREATE INDEX'));
console.log(JSON.stringify(body), '=>', JSON.stringify(cols));
for (const l of ddl) console.log(' ', l);
}Output:
"a b" => [["a","b"]]
CREATE INDEX ON "t" ("a", "b");
"(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)\n (b)" => [["a"],["b"]]
CREATE INDEX ON "t" ("a");
CREATE INDEX ON "t" ("b");
"(a, b)" => [["a","b"]]
CREATE INDEX ON "t" ("a", "b");
More results from the same harness, on the same table:
indexes body |
Result |
|---|---|
a ⏎ b |
2 indexes: [a], [b] |
a, TAB, b |
1 index: [a, b] |
a b c |
1 index: [a, b, c] |
(a) b |
1 index: [a, b] |
a b [unique] |
1 unique index: [a, b]. The setting applies to the whole entry |
(a, ⏎ b) |
1 index: [a, b] |
a, b |
Error: An index field must be an identifier, a quoted identifier, a functional expression or a tuple of such |
(a b) |
Error: Expect a comma ',' |
What the docs say
Index Definition gives examples but no grammar. Every multi-field example is a parenthesized, comma-separated tuple, and every entry is on its own line:
indexes {
(id, country) [pk] // composite primary key
created_at [name: 'created_at_index', note: 'Date']
booking_date
(country, booking_date) [unique]
booking_date [type: hash]
(`id*2`)
(`id*3`,`getdate()`)
(`id*3`,id)
}
The examples don't show what a b or (a) (b) on one line should mean. They could be one composite index, two indexes, or an error.
What the implementation appears to do
This is our reading of the source and the results above. Please correct us if any of it is wrong.
- Outside parentheses, an entry ends at a newline.
(a,⏎b)is still one entry. A(at the start of a line begins a new entry, which is why(a)⏎(b)gives two indexes. - Within one line, fields separated by spaces or tabs become the arguments of one
FunctionApplicationNode.interpret.tscollects the callee and all arguments into the columns of a single index. - A field written directly before
(…), as ina (b)or(a) (b), is parsed as aCallExpressionNode. The flatten step then reverses the field order. For the related reorder of expression fields, see Index columns change order when a functional expression comes after a column #1005. - In the cases we tried,
(a, b)and(a,⏎b)are accepted.a, b(a comma outside parentheses) and(a b)(no comma inside parentheses) are rejected.
The comment in interpret.ts (// Flatten call expressions like (id, name)(age, weight)) suggests that at least the juxtaposed-tuple form is intended.
Why this matters
- Composite index column order matters because it affects how an index can be used and query performance. Reading
a bas one index or as two changes both the number of indexes and their columns. Because the docs don't define the grammar, they don't settle which reading is correct. - Recent work touches the same boundary: A line starting with
-continues the expression on the line above #1006 / fix(parser): parse a block line starting with an operator as its own line #1007 (whether a line continues the expression above). A documented entry grammar would give that work a fixed reference point.
Requests
- Clarify the intended grammar of an index entry. Several fields on one line, separated by spaces or tabs, could mean one composite index, separate indexes, or an error. We leave that choice to you, and would like the docs to state it.
- Whichever reading you choose, it would help if the docs also stated:
- what ends an entry (a newline?)
- how bare fields and tuples combine on one line (
(a, b) c,a (b, c),(a) (b)) - which fields a trailing
[...]setting applies to - where a comma is allowed
- If the current composite reading is kept, the field order should follow the source order (see Index columns change order when a functional expression comes after a column #1005).
Activity
huydo862003 commentedon Oct 19, 2026
Hi @kojifukada, thank you for your report.
Regarding the out-of-order index column issue, we'll release a fix in the next few days.
Regarding the behavior where index entries separated by whitespaces form a composite one, it is indeed an oversight. Since we don't specify this syntax in the document, we suggest you follow the documented syntax: Always separate different indexes with a newline. Composite indexes are enclosed in
()like python tuples.This legacy, ambiguous syntax shall be deprecated in the near future.
kojifukada commentedon Oct 19, 2026
Thanks for the clarification, @huydo862003, and for the upcoming fix!
This is very helpful for us as a downstream consumer. We'll follow the documented syntax.