Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

has_one children being updated with parent record #68

Open
georgedrummond opened this issue Jun 3, 2015 · 1 comment
Open

has_one children being updated with parent record #68

georgedrummond opened this issue Jun 3, 2015 · 1 comment

Comments

@georgedrummond
Copy link

There is currently an issue on rails 4.2 and mysql where children of a has_one relationship are having their UUID updated at the same time as the parent. The strange thing is that a mysql UPDATE query is run overwriting the exact same data.

To replicate the issue I have created a dummy app with a failing test suite. In the example we have Owner has_one Dog which does not use activeuuid and displays the rails default behaviour. We also have User has_one Car which does use uuid.

If we first take Owner has_one Dog we can confirm that the Dog record is never touched when updating the Owner.

o = Owner.create
   (0.1ms)  BEGIN
  SQL (0.2ms)  INSERT INTO `owners` (`created_at`, `updated_at`) VALUES ('2015-06-03 21:05:23', '2015-06-03 21:05:23')
   (0.2ms)  COMMIT
=> #<Owner id: 1, name: nil, created_at: "2015-06-03 21:05:23", updated_at: "2015-06-03 21:05:23">
irb(main):005:0> o.create_dog
   (0.1ms)  BEGIN
  SQL (0.2ms)  INSERT INTO `dogs` (`owner_id`, `created_at`, `updated_at`) VALUES (1, '2015-06-03 21:05:27', '2015-06-03 21:05:27')
   (0.3ms)  COMMIT
  Dog Load (0.2ms)  SELECT  `dogs`.* FROM `dogs` WHERE `dogs`.`owner_id` = 1 LIMIT 1
=> #<Dog id: 1, owner_id: 1, created_at: "2015-06-03 21:05:27", updated_at: "2015-06-03 21:05:27">
irb(main):006:0> o.update name: 'George'
   (0.2ms)  BEGIN
  SQL (0.5ms)  UPDATE `owners` SET `name` = 'George', `updated_at` = '2015-06-03 21:05:34' WHERE `owners`.`id` = 1
   (6.4ms)  COMMIT
=> true

Next lets try again with User has_one Car which does use activeuuid. Our Car record should not be touched when updating the Owner.

u = User.create
   (0.1ms)  BEGIN
  SQL (0.2ms)  INSERT INTO `users` (`id`, `created_at`, `updated_at`) VALUES (x'69704d2f29cd4bc484fa9545b8c54a55', '2015-06-03 21:04:54', '2015-06-03 21:04:54')
   (0.2ms)  COMMIT
=> #<User id: #<UUID:0x3ff7ae5768f0 UUID:69704d2f-29cd-4bc4-84fa-9545b8c54a55>, name: nil, created_at: "2015-06-03 21:04:54", updated_at: "2015-06-03 21:04:54">
irb(main):002:0> c = u.create_car
   (0.1ms)  BEGIN
  SQL (0.2ms)  INSERT INTO `cars` (`id`, `user_id`, `created_at`, `updated_at`) VALUES (x'f92659de32814c7f8e05d95bc13fab72', x'69704d2f29cd4bc484fa9545b8c54a55', '2015-06-03 21:05:03', '2015-06-03 21:05:03')
   (0.3ms)  COMMIT
  Car Load (0.2ms)  SELECT  `cars`.* FROM `cars` WHERE `cars`.`user_id` = x'69704d2f29cd4bc484fa9545b8c54a55' LIMIT 1
=> #<Car id: #<UUID:0x3ff7adb9276c UUID:f92659de-3281-4c7f-8e05-d95bc13fab72>, user_id: #<UUID:0x3ff7ae5768f0 UUID:69704d2f-29cd-4bc4-84fa-9545b8c54a55>, make: nil, created_at: "2015-06-03 21:05:03", updated_at: "2015-06-03 21:05:03">
irb(main):003:0> u.update name: 'George'
   (0.2ms)  BEGIN
  SQL (0.4ms)  UPDATE `users` SET `id` = x'69704d2f29cd4bc484fa9545b8c54a55', `name` = 'George', `updated_at` = '2015-06-03 21:05:15' WHERE `users`.`id` = x'69704d2f29cd4bc484fa9545b8c54a55'

  ######################################

  SQL (0.2ms)  UPDATE `cars` SET `id` = x'f92659de32814c7f8e05d95bc13fab72', `user_id` = x'69704d2f29cd4bc484fa9545b8c54a55', `updated_at` = '2015-06-03 21:05:15' WHERE `cars`.`id` = x'f92659de32814c7f8e05d95bc13fab72'

  ######################################

   (6.2ms)  COMMIT
=> true

From the logs we can see that the Car had its id and user_id updated along with the timestamps.

Has anyone else experienced this problem or come up with a fix?

@georgedrummond
Copy link
Author

I think this issue is related to the UUID being a serialized object

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant