Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion d2compiler/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,7 @@ func (c *compiler) compileSQLTable(obj *d2graph.Object) {
obj.SQLTable = &d2target.SQLTable{}
for _, col := range obj.ChildrenArray {
typ := col.Label.Value
if typ == col.IDVal {
if typ == col.IDVal && !hasExplicitScalarLabel(col.Label.MapKey) {
// Not great, AST should easily allow specifying alternate primary field
// as an explicit label should change the name.
typ = ""
Expand All @@ -1086,6 +1086,17 @@ func (c *compiler) compileSQLTable(obj *d2graph.Object) {
obj.ChildrenArray = nil
}

func hasExplicitScalarLabel(mapKey *d2ast.Key) bool {
if mapKey == nil {
return false
}
if mapKey.Primary.Unbox() != nil {
return true
}
_, ok := mapKey.Value.Unbox().(d2ast.Scalar)
return ok
}

func (c *compiler) validateKeys(obj *d2graph.Object, m *d2ir.Map) {
for _, f := range m.Fields {
if _, ok := d2ast.BoardKeywords[f.Name.ScalarString()]; ok && f.Name.IsUnquoted() {
Expand Down
18 changes: 18 additions & 0 deletions d2compiler/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2743,6 +2743,24 @@ ok: {
tassert.Equal(t, []string{"primary_key", "foreign_key"}, table.Columns[1].Constraint)
},
},
{
name: "sql-column-type-matches-name",
text: `x: {
shape: sql_table
date: date
id
created {constraint: primary_key}
}`,
assertions: func(t *testing.T, g *d2graph.Graph) {
table := g.Objects[0].SQLTable
tassert.Equal(t, "date", table.Columns[0].Name.Label)
tassert.Equal(t, "date", table.Columns[0].Type.Label)
tassert.Equal(t, "id", table.Columns[1].Name.Label)
tassert.Empty(t, table.Columns[1].Type.Label)
tassert.Equal(t, "created", table.Columns[2].Name.Label)
tassert.Empty(t, table.Columns[2].Type.Label)
},
},
{
name: "sql-null-constraint",
text: `x: {
Expand Down
Loading