Skip to content

Commit

Permalink
add failing test for long foreign keys in mysql
Browse files Browse the repository at this point in the history
  • Loading branch information
nkovacs committed May 21, 2016
1 parent 57c7212 commit ed9cfac
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
21 changes: 20 additions & 1 deletion association_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down Expand Up @@ -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))
}
}
8 changes: 7 additions & 1 deletion migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit ed9cfac

Please sign in to comment.