sqlboiler/bdb/relationships.go

48 lines
1.1 KiB
Go
Raw Normal View History

package bdb
2016-06-22 22:03:05 -07:00
// ToManyRelationship describes a relationship between two tables where the
// local table has no id, and the foreign table has an id that matches a column
// in the local table.
type ToManyRelationship struct {
Column string
2016-06-22 22:03:05 -07:00
ForeignTable string
ForeignColumn string
}
// ToManyRelationships relationship lookups
// Input should be the sql name of a table like: videos
func ToManyRelationships(table string, tables []Table) []ToManyRelationship {
var relationships []ToManyRelationship
for _, t := range tables {
if t.Name == table {
continue
}
for _, f := range t.FKeys {
if f.ForeignTable != table {
continue
}
// singularName := strmangle.Singular(table)
// standardColName := fmt.Sprintf("%s_id", singularName)
2016-06-22 22:03:05 -07:00
relationship := ToManyRelationship{
Column: f.ForeignColumn,
2016-06-22 22:03:05 -07:00
ForeignTable: t.Name,
ForeignColumn: f.Column,
}
// if standardColName == f.ForeignColumn {
// relationship.Name = table
// } else {
// relationship.Name = table
// }
2016-06-22 22:03:05 -07:00
relationships = append(relationships, relationship)
}
}
return relationships
}