Skip to content

Commit

Permalink
improved one to many constraint detection for mysql
Browse files Browse the repository at this point in the history
  • Loading branch information
KarnerTh committed Jan 29, 2022
1 parent 1618af1 commit a65267f
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions database/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,15 @@ func (c mysqlConnector) GetConstraints(tableName string) ([]ConstraintResult, er
ON kc.COLUMN_NAME = kc2.COLUMN_NAME AND kc2.CONSTRAINT_NAME = 'PRIMARY' AND
kc2.TABLE_NAME = c.TABLE_NAME
where kc.CONSTRAINT_NAME = c.CONSTRAINT_NAME
) "isPrimary"
) "isPrimary",
(
select COUNT(*) > 1
from information_schema.KEY_COLUMN_USAGE kc
where kc.TABLE_NAME = c.TABLE_NAME
and kc.CONSTRAINT_NAME = 'PRIMARY'
) "hasMultiplePk"
from information_schema.REFERENTIAL_CONSTRAINTS c
where c.TABLE_NAME = ?
or c.REFERENCED_TABLE_NAME = ?
where c.TABLE_NAME = ? or c.REFERENCED_TABLE_NAME = ?
`, tableName, tableName)
if err != nil {
return nil, err
Expand All @@ -123,7 +128,14 @@ func (c mysqlConnector) GetConstraints(tableName string) ([]ConstraintResult, er
var constraints []ConstraintResult
for rows.Next() {
var constraint ConstraintResult
if err = rows.Scan(&constraint.FkTable, &constraint.PKTable, &constraint.ConstraintName, &constraint.IsPrimary); err != nil {
err = rows.Scan(&constraint.FkTable,
&constraint.PKTable,
&constraint.ConstraintName,
&constraint.IsPrimary,
&constraint.HasMultiplePK,
)

if err != nil {
return nil, err
}

Expand Down

0 comments on commit a65267f

Please sign in to comment.