Be judicious about fkey naming

This commit is contained in:
Aaron L 2016-09-22 22:11:30 -07:00
parent 0530ba9227
commit 6401a277cf
2 changed files with 8 additions and 4 deletions

View file

@ -196,7 +196,7 @@ func txtsFromToMany(tables []bdb.Table, table bdb.Table, rel bdb.ToManyRelations
// fk == table = industry.Industry | industry.Industry // fk == table = industry.Industry | industry.Industry
// fk != table = industry.ParentIndustry | industry.Industry // fk != table = industry.ParentIndustry | industry.Industry
func txtNameToOne(fk bdb.ForeignKey) (localFn, foreignFn string) { func txtNameToOne(fk bdb.ForeignKey) (localFn, foreignFn string) {
localFn = trimSuffixes(fk.Column) localFn = strmangle.Singular(trimSuffixes(fk.Column))
fkeyIsTableName := localFn != strmangle.Singular(fk.ForeignTable) fkeyIsTableName := localFn != strmangle.Singular(fk.ForeignTable)
localFn = strmangle.TitleCase(localFn) localFn = strmangle.TitleCase(localFn)
@ -235,8 +235,8 @@ func txtNameToOne(fk bdb.ForeignKey) (localFn, foreignFn string) {
// fk != table = industry.MappedIndustryIndustry // fk != table = industry.MappedIndustryIndustry
func txtNameToMany(toMany bdb.ToManyRelationship) (localFn, foreignFn string) { func txtNameToMany(toMany bdb.ToManyRelationship) (localFn, foreignFn string) {
if toMany.ToJoinTable { if toMany.ToJoinTable {
localFkey := trimSuffixes(toMany.JoinLocalColumn) localFkey := strmangle.Singular(trimSuffixes(toMany.JoinLocalColumn))
foreignFkey := trimSuffixes(toMany.JoinForeignColumn) foreignFkey := strmangle.Singular(trimSuffixes(toMany.JoinForeignColumn))
if localFkey != strmangle.Singular(toMany.Table) { if localFkey != strmangle.Singular(toMany.Table) {
foreignFn = strmangle.TitleCase(localFkey) foreignFn = strmangle.TitleCase(localFkey)
@ -251,7 +251,7 @@ func txtNameToMany(toMany bdb.ToManyRelationship) (localFn, foreignFn string) {
return localFn, foreignFn return localFn, foreignFn
} }
fkeyName := trimSuffixes(toMany.ForeignColumn) fkeyName := strmangle.Singular(trimSuffixes(toMany.ForeignColumn))
if fkeyName != strmangle.Singular(toMany.Table) { if fkeyName != strmangle.Singular(toMany.Table) {
localFn = strmangle.TitleCase(fkeyName) localFn = strmangle.TitleCase(fkeyName)
} }

View file

@ -190,6 +190,8 @@ func TestTxtNameToOne(t *testing.T) {
{"jets", "jet_id", true, "jets", "id", true, "Jet", "Jet"}, {"jets", "jet_id", true, "jets", "id", true, "Jet", "Jet"},
{"jets", "plane_id", false, "jets", "id", true, "Plane", "PlaneJets"}, {"jets", "plane_id", false, "jets", "id", true, "Plane", "PlaneJets"},
{"jets", "plane_id", true, "jets", "id", true, "Plane", "PlaneJet"}, {"jets", "plane_id", true, "jets", "id", true, "Plane", "PlaneJet"},
{"race_result_scratchings", "results_id", false, "race_results", "id", true, "Result", "ResultRaceResultScratchings"},
} }
for i, test := range tests { for i, test := range tests {
@ -237,6 +239,8 @@ func TestTxtNameToMany(t *testing.T) {
{"pilots", "id", "pilots", "id", true, "pilot_id", "mentor_id", "MentorPilots", "Pilots"}, {"pilots", "id", "pilots", "id", true, "pilot_id", "mentor_id", "MentorPilots", "Pilots"},
{"pilots", "id", "pilots", "id", true, "mentor_id", "pilot_id", "Pilots", "MentorPilots"}, {"pilots", "id", "pilots", "id", true, "mentor_id", "pilot_id", "Pilots", "MentorPilots"},
{"pilots", "id", "pilots", "id", true, "captain_id", "mentor_id", "MentorPilots", "CaptainPilots"}, {"pilots", "id", "pilots", "id", true, "captain_id", "mentor_id", "MentorPilots", "CaptainPilots"},
{"race_results", "id", "race_result_scratchings", "results_id", false, "", "", "ResultRaceResultScratchings", "Result"},
} }
for i, test := range tests { for i, test := range tests {