Add even more nullable things for FKeys.

This commit is contained in:
Aaron L 2016-07-12 08:09:26 -07:00
commit 7cdf44376b
3 changed files with 42 additions and 30 deletions

View file

@ -52,11 +52,15 @@ func Tables(db Interface, names ...string) ([]Table, error) {
}
setIsJoinTable(&t)
setForeignKeyNullability(&t)
tables = append(tables, t)
}
// Relationships have a dependency on foreign key nullability.
for i := range tables {
tbl := &tables[i]
setForeignKeyNullability(tbl, tables)
}
for i := range tables {
tbl := &tables[i]
setRelationships(tbl, tables)
@ -89,22 +93,14 @@ func setIsJoinTable(t *Table) {
t.IsJoinTable = true
}
func setForeignKeyNullability(t *Table) {
func setForeignKeyNullability(t *Table, tables []Table) {
for i, fkey := range t.FKeys {
localColumn := t.GetColumn(fkey.Column)
foreignTable := GetTable(tables, fkey.ForeignTable)
foreignColumn := foreignTable.GetColumn(fkey.ForeignColumn)
found := -1
for j, col := range t.Columns {
if col.Name == fkey.Column {
found = j
break
}
}
if found < 0 {
panic("could not find foreign key column in table")
}
t.FKeys[i].Nullable = t.Columns[found].Nullable
t.FKeys[i].Nullable = localColumn.Nullable
t.FKeys[i].ForeignColumnNullable = foreignColumn.Nullable
}
}