add failing test for long foreign keys in mysql

This commit is contained in:
Nikola Kovacs 2016-05-21 19:12:02 +02:00
parent 57c72125b3
commit ed9cfac496
2 changed files with 27 additions and 2 deletions

View File

@ -809,7 +809,7 @@ func TestRelated(t *testing.T) {
func TestForeignKey(t *testing.T) { func TestForeignKey(t *testing.T) {
for _, structField := range DB.NewScope(&User{}).GetStructFields() { 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 { if structField.Name == foreignKey && !structField.IsForeignKey {
t.Errorf(fmt.Sprintf("%v should be foreign key", foreignKey)) 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))
}
}

View File

@ -37,6 +37,12 @@ type User struct {
IgnoreStringSlice []string `sql:"-"` IgnoreStringSlice []string `sql:"-"`
Ignored struct{ Name string } `sql:"-"` Ignored struct{ Name string } `sql:"-"`
IgnoredPointer *User `sql:"-"` IgnoredPointer *User `sql:"-"`
ReallyLongThingID int64
ReallyLongThing ReallyLongTableNameToTestMySQLNameLengthLimit
}
type ReallyLongTableNameToTestMySQLNameLengthLimit struct {
Id int64
} }
type CreditCard struct { type CreditCard struct {
@ -231,7 +237,7 @@ func runMigration() {
DB.Exec(fmt.Sprintf("drop table %v;", table)) 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 { for _, value := range values {
DB.DropTable(value) DB.DropTable(value)
} }