| 518 | } |
| 519 | |
| 520 | func TestUpdateColumnsSkipsAssociations(t *testing.T) { |
| 521 | user := *GetUser("update_column_skips_association", Config{}) |
| 522 | DB.Create(&user) |
| 523 | |
| 524 | // Update a single field of the user and verify that the changed address is not stored. |
| 525 | newAge := uint(100) |
| 526 | user.Account.Number = "new_account_number" |
| 527 | db := DB.Model(&user).UpdateColumns(User{Age: newAge}) |
| 528 | |
| 529 | if db.RowsAffected != 1 { |
| 530 | t.Errorf("Expected RowsAffected=1 but instead RowsAffected=%v", db.RowsAffected) |
| 531 | } |
| 532 | |
| 533 | // Verify that Age now=`newAge`. |
| 534 | result := &User{} |
| 535 | result.ID = user.ID |
| 536 | DB.Preload("Account").First(result) |
| 537 | |
| 538 | if result.Age != newAge { |
| 539 | t.Errorf("Expected freshly queried user to have Age=%v but instead found Age=%v", newAge, result.Age) |
| 540 | } |
| 541 | |
| 542 | if result.Account.Number != user.Account.Number { |
| 543 | t.Errorf("account number should not been changed, expects: %v, got %v", user.Account.Number, result.Account.Number) |
| 544 | } |
| 545 | } |
| 546 | |
| 547 | func TestUpdatesWithBlankValues(t *testing.T) { |
| 548 | user := *GetUser("updates_with_blank_value", Config{}) |