Is there a way to circumvent the standard naming conventions for column/field names of foreign keys? For example, the following schema works for me:
type Clown struct {
ID uint `gorm:"PRIMARY_KEY;not null;AUTO_INCREMENT"`
Name string `gorm:"type:varchar(50);unique_index;not null"`
Circus Circus `gorm:"ForeignKey:CircusID;AssociationForeignKey:ID`
CircusID uint `gorm:"not null" sql:"type:integer REFERENCES circus(id)"`
}
type Circus struct {
ID uint `gorm:"PRIMARY_KEY;not null;AUTO_INCREMENT"`
Name string `gorm:"type:varchar(50);unique_index;not null"`
Clowns []Clown
}
However, if I rename the Clown's "CircusID" field/column to "Foo", it no longer works:
type Clown struct {
ID uint `gorm:"PRIMARY_KEY;not null;AUTO_INCREMENT"`
Name string `gorm:"type:varchar(50);unique_index;not null"`
Circus Circus `gorm:"ForeignKey:Foo;AssociationForeignKey:ID`
Foo uint `gorm:"not null" sql:"type:integer REFERENCES circus(id)"`
}
I get an error "invalid association []". It is important to me that I stick with the "Foo" column name since I am working with an existing database and don't want to go through the trouble of changing the name of the column. Am I missing something? Is there a way to properly specify a foreign key without calling it "{{Tablename}}ID"?