Skip to content
Merged
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
17 changes: 14 additions & 3 deletions cmd/sql2diagram/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,21 @@ func alterTableStmt(schema *Schema, stmt *pgQuery.AlterTableStmt) error {
Table: constraint.Constraint.Pktable.Relname,
}

// Get the referenced column name from PkAttrs (primary key attributes)
for _, pkattr := range constraint.Constraint.PkAttrs {
node, ok := pkattr.Node.(*pgQuery.Node_String_)
if !ok {
continue
}
foreignReference.Column = node.String_.Sval
}

// Get the foreign key column name from FkAttrs (foreign key attributes)
for _, fkattr := range constraint.Constraint.FkAttrs {
node, ok := fkattr.Node.(*pgQuery.Node_String_)
if !ok {
continue
}

var column *Column
for _, col := range sourceTable.Columns {
Expand All @@ -245,9 +255,10 @@ func alterTableStmt(schema *Schema, stmt *pgQuery.AlterTableStmt) error {
}
}

foreignReference.Column = node.String_.Sval

column.ForeignKeyReferences = append(column.ForeignKeyReferences, foreignReference)
// Check if column was found before trying to append to it
if column != nil {
column.ForeignKeyReferences = append(column.ForeignKeyReferences, foreignReference)
}
}
}

Expand Down