d2lang/d2 · error
dimensions for sql_table constraint %#v not found
Error message
dimensions for sql_table constraint %#v not found
What it means
If a sql_table column declares a constraint (e.g. PRIMARY KEY, NOT NULL), its text (ctexts[2]) must be measured to compute the constraint column width. A nil measurement — not pre-measured and not ruler-measurable — fails the whole sql_table sizing with this error.
Source
Thrown at d2graph/d2graph.go:1116
if nameDims == nil {
return nil, fmt.Errorf("dimensions for sql_table name %#v not found", ctexts[0].Text)
}
c.Name.LabelWidth = nameDims.Width
c.Name.LabelHeight = nameDims.Height
maxNameWidth = go2.Max(maxNameWidth, nameDims.Width)
typeDims := GetTextDimensions(mtexts, ruler, ctexts[1], fontFamily)
if typeDims == nil {
return nil, fmt.Errorf("dimensions for sql_table type %#v not found", ctexts[1].Text)
}
c.Type.LabelWidth = typeDims.Width
c.Type.LabelHeight = typeDims.Height
maxTypeWidth = go2.Max(maxTypeWidth, typeDims.Width)
if l := len(c.Constraint); l > 0 {
constraintDims := GetTextDimensions(mtexts, ruler, ctexts[2], fontFamily)
if constraintDims == nil {
return nil, fmt.Errorf("dimensions for sql_table constraint %#v not found", ctexts[2].Text)
}
maxConstraintWidth = go2.Max(maxConstraintWidth, constraintDims.Width)
}
}
// The rows get padded a little due to header font being larger than row font
dims.Height = go2.Max(12, labelDims.Height*(len(obj.SQLTable.Columns)+1))
headerWidth := d2target.HeaderPadding + labelDims.Width + d2target.HeaderPadding
rowsWidth := d2target.NamePadding + maxNameWidth + d2target.TypePadding + maxTypeWidth + d2target.TypePadding + maxConstraintWidth
if maxConstraintWidth != 0 {
rowsWidth += d2target.ConstraintPadding
}
dims.Width = go2.Max(12, go2.Max(headerWidth, rowsWidth))
}
return &dims, nil
}
View on GitHub (pinned to 0d69dca6f5)
Solutions
- Pass a non-nil, font-loaded textmeasure.Ruler to SetDimensions
- Regenerate mtexts with graph.SetText after modifying column constraints
- Verify the ruler loaded the font family used for constraint text
- Build sql_tables through the standard d2 compile flow rather than hand-constructing them
Example fix
// before err := graph.SetDimensions(mtexts, nil, fontFamily, nil) // after ruler, _ := textmeasure.NewRuler() ruler.InitFonts(d2fonts.D2Fonts) err := graph.SetDimensions(mtexts, ruler, fontFamily, &d2fonts.SourceCodePro)
Defensive patterns
Strategy: validation
Validate before calling
ruler, _ := textmeasure.NewRuler()
ruler.InitFonts(d2fonts.D2Fonts)
for _, obj := range graph.Objects {
if obj.SQLTable != nil {
for _, c := range obj.SQLTable.Columns {
if c.Constraint == "" { continue }
txt := c.Texts(d2fonts.DEFAULT_FONT_SIZE)[2].Text
found := false
for _, mt := range mtexts { if mt.Text == txt { found = true; break } }
if !found { return fmt.Errorf("unmeasured sql_table constraint: %q", txt) }
}
}
} Type guard
func sqlTableConstraintsMeasured(obj *d2graph.Object, mtexts []*d2target.MText) bool {
if obj.SQLTable == nil { return true }
for _, c := range obj.SQLTable.Columns {
if c.Constraint == "" { continue }
txt := c.Texts(d2fonts.DEFAULT_FONT_SIZE)[2].Text
found := false
for _, mt := range mtexts { if mt.Text == txt { found = true; break } }
if !found { return false }
}
return true
} Try / catch
if err := graph.SetDimensions(mtexts, ruler, fontFamily, mono); err != nil {
if strings.Contains(err.Error(), "dimensions for sql_table constraint") {
mtexts = graph.SetText(ruler, fontFamily)
err = graph.SetDimensions(mtexts, ruler, fontFamily, mono)
}
return err
} Prevention
- Add constraints before running the measurement pass, never after
- Keep a font-initialized ruler in scope for all SetDimensions calls
- Regenerate mtexts after any constraint edit
- Test sql_table rendering in CI with constraints to catch stale measurement lists
When it happens
Trigger: Rendering a sql_table with a non-empty column.Constraint whose constraint text at colFontSize is absent from mtexts and cannot be measured (nil ruler or missing measurement).
Common situations: Constraints added to columns after mtexts were generated; nil ruler in embedded renderers; custom font family not loaded into ruler; programmatic table construction skipping the measurement pass.
Related errors
- dimensions for sql_table name %#v not found
- dimensions for sql_table type %#v not found
- dimensions for object label %#v not found
- dimensions for class field %#v not found
- dimensions for class method %#v not found
AI-assisted analysis of d2lang/d2@0d69dca6f5 (2026-08-31).
Data as JSON: /api/errors/b1ef84c995563851.
Report an issue: GitHub.