Fix generation of self many-to-many

- Clean up the generation of to_many relationship types
This commit is contained in:
Aaron L 2016-09-21 19:48:24 -07:00
parent b0cdc29c61
commit 14c5e5f149
2 changed files with 28 additions and 29 deletions

View file

@ -76,7 +76,7 @@ func toManyRelationships(table Table, tables []Table) []ToManyRelationship {
for _, t := range tables {
for _, f := range t.FKeys {
if f.ForeignTable == table.Name && !f.Unique {
if f.ForeignTable == table.Name && (t.IsJoinTable || !f.Unique) {
relationships = append(relationships, buildToManyRelationship(table, f, t, tables))
}
}
@ -101,12 +101,11 @@ func buildToOneRelationship(localTable Table, foreignKey ForeignKey, foreignTabl
func buildToManyRelationship(localTable Table, foreignKey ForeignKey, foreignTable Table, tables []Table) ToManyRelationship {
if !foreignTable.IsJoinTable {
col := localTable.GetColumn(foreignKey.ForeignColumn)
return ToManyRelationship{
Table: localTable.Name,
Column: foreignKey.ForeignColumn,
Nullable: col.Nullable,
Unique: col.Unique,
Nullable: foreignKey.ForeignColumnNullable,
Unique: foreignKey.ForeignColumnUnique,
ForeignTable: foreignTable.Name,
ForeignColumn: foreignKey.Column,
ForeignColumnNullable: foreignKey.Nullable,
@ -115,33 +114,33 @@ func buildToManyRelationship(localTable Table, foreignKey ForeignKey, foreignTab
}
}
col := foreignTable.GetColumn(foreignKey.Column)
relationship := ToManyRelationship{
Table: localTable.Name,
Column: foreignKey.ForeignColumn,
Nullable: col.Nullable,
Unique: col.Unique,
Nullable: foreignKey.ForeignColumnNullable,
Unique: foreignKey.ForeignColumnUnique,
ToJoinTable: true,
JoinTable: foreignTable.Name,
JoinLocalColumn: foreignKey.Column,
JoinLocalColumnNullable: foreignKey.Nullable,
JoinLocalColumnUnique: foreignKey.Unique,
}
for _, fk := range foreignTable.FKeys {
if fk.ForeignTable != localTable.Name {
if fk.Name == foreignKey.Name {
continue
}
relationship.JoinForeignColumn = fk.Column
relationship.JoinForeignColumnNullable = fk.Nullable
relationship.JoinForeignColumnUnique = fk.Unique
foreignTable := GetTable(tables, fk.ForeignTable)
foreignCol := foreignTable.GetColumn(fk.ForeignColumn)
relationship.ForeignTable = fk.ForeignTable
relationship.ForeignColumn = fk.ForeignColumn
relationship.ForeignColumnNullable = foreignCol.Nullable
relationship.ForeignColumnUnique = foreignCol.Unique
} else {
relationship.JoinLocalColumn = fk.Column
relationship.JoinLocalColumnNullable = fk.Nullable
relationship.JoinLocalColumnUnique = fk.Unique
}
relationship.ForeignColumnNullable = fk.ForeignColumnNullable
relationship.ForeignColumnUnique = fk.ForeignColumnUnique
}
return relationship

View file

@ -213,15 +213,15 @@ func TestToManyRelationshipsNull(t *testing.T) {
Name: "jets",
Columns: []Column{{Name: "id", Nullable: true}, {Name: "pilot_id", Nullable: true}, {Name: "airport_id", Nullable: true}},
FKeys: []ForeignKey{
{Name: "jets_pilot_id_fk", Column: "pilot_id", ForeignTable: "pilots", ForeignColumn: "id", Nullable: true},
{Name: "jets_airport_id_fk", Column: "airport_id", ForeignTable: "airports", ForeignColumn: "id", Nullable: true},
{Name: "jets_pilot_id_fk", Column: "pilot_id", ForeignTable: "pilots", ForeignColumn: "id", Nullable: true, ForeignColumnNullable: true},
{Name: "jets_airport_id_fk", Column: "airport_id", ForeignTable: "airports", ForeignColumn: "id", Nullable: true, ForeignColumnNullable: true},
},
},
{
Name: "licenses",
Columns: []Column{{Name: "id", Nullable: true}, {Name: "pilot_id", Nullable: true}},
FKeys: []ForeignKey{
{Name: "licenses_pilot_id_fk", Column: "pilot_id", ForeignTable: "pilots", ForeignColumn: "id", Nullable: true},
{Name: "licenses_pilot_id_fk", Column: "pilot_id", ForeignTable: "pilots", ForeignColumn: "id", Nullable: true, ForeignColumnNullable: true},
},
},
{
@ -237,8 +237,8 @@ func TestToManyRelationshipsNull(t *testing.T) {
IsJoinTable: true,
Columns: []Column{{Name: "pilot_id", Nullable: true}, {Name: "language_id", Nullable: true}},
FKeys: []ForeignKey{
{Name: "pilot_id_fk", Column: "pilot_id", ForeignTable: "pilots", ForeignColumn: "id", Nullable: true},
{Name: "language_id_fk", Column: "language_id", ForeignTable: "languages", ForeignColumn: "id", Nullable: true},
{Name: "pilot_id_fk", Column: "pilot_id", ForeignTable: "pilots", ForeignColumn: "id", Nullable: true, ForeignColumnNullable: true},
{Name: "language_id_fk", Column: "language_id", ForeignTable: "languages", ForeignColumn: "id", Nullable: true, ForeignColumnNullable: true},
},
},
}