diff --git a/association_test.go b/association_test.go index 52d2303f..e4aabb43 100644 --- a/association_test.go +++ b/association_test.go @@ -809,7 +809,7 @@ func TestRelated(t *testing.T) { func TestForeignKey(t *testing.T) { for _, structField := range DB.NewScope(&User{}).GetStructFields() { - for _, foreignKey := range []string{"BillingAddressID", "ShippingAddressId", "CompanyID"} { + for _, foreignKey := range []string{"BillingAddressID", "ShippingAddressId", "CompanyID", "ReallyLongThingID"} { if structField.Name == foreignKey && !structField.IsForeignKey { t.Errorf(fmt.Sprintf("%v should be foreign key", foreignKey)) } @@ -840,3 +840,22 @@ func TestForeignKey(t *testing.T) { } } } + +func TestLongForeignKey(t *testing.T) { + targetScope := DB.NewScope(&ReallyLongTableNameToTestMySQLNameLengthLimit{}) + targetTableName := targetScope.TableName() + modelScope := DB.NewScope(&User{}) + modelField, ok := modelScope.FieldByName("ReallyLongThingID") + if !ok { + t.Fatalf("Failed to get field by name: ReallyLongThingID") + } + targetField, ok := targetScope.FieldByName("ID") + if !ok { + t.Fatalf("Failed to get field by name: ID") + } + dest := fmt.Sprintf("%v(%v)", targetTableName, targetField.DBName) + err := DB.Model(&User{}).AddForeignKey(modelField.DBName, dest, "CASCADE", "CASCADE").Error + if err != nil { + t.Fatalf(fmt.Sprintf("Failed to create foreign key: %v", err)) + } +} diff --git a/migration_test.go b/migration_test.go index 38e5c1c2..fc692bcc 100644 --- a/migration_test.go +++ b/migration_test.go @@ -37,6 +37,12 @@ type User struct { IgnoreStringSlice []string `sql:"-"` Ignored struct{ Name string } `sql:"-"` IgnoredPointer *User `sql:"-"` + ReallyLongThingID int64 + ReallyLongThing ReallyLongTableNameToTestMySQLNameLengthLimit +} + +type ReallyLongTableNameToTestMySQLNameLengthLimit struct { + Id int64 } type CreditCard struct { @@ -231,7 +237,7 @@ func runMigration() { DB.Exec(fmt.Sprintf("drop table %v;", table)) } - values := []interface{}{&Product{}, &Email{}, &Address{}, &CreditCard{}, &Company{}, &Role{}, &Language{}, &HNPost{}, &EngadgetPost{}, &Animal{}, &User{}, &JoinTable{}, &Post{}, &Category{}, &Comment{}, &Cat{}, &Dog{}, &Toy{}} + values := []interface{}{&ReallyLongTableNameToTestMySQLNameLengthLimit{}, &Product{}, &Email{}, &Address{}, &CreditCard{}, &Company{}, &Role{}, &Language{}, &HNPost{}, &EngadgetPost{}, &Animal{}, &User{}, &JoinTable{}, &Post{}, &Category{}, &Comment{}, &Cat{}, &Dog{}, &Toy{}} for _, value := range values { DB.DropTable(value) }