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

Joined tables problem #57

Closed
alexturetsky opened this issue Apr 24, 2020 · 10 comments
Closed

Joined tables problem #57

alexturetsky opened this issue Apr 24, 2020 · 10 comments
Assignees
Labels

Comments

@alexturetsky
Copy link

I have two tables - User and Devices:

class User < ActiveRecord::Base
  include ActiveUUID::Model
  attribute :id, :uuid
  has_many :devices
end
class Device < ActiveRecord::Base
  include ActiveUUID::Model
  attribute :id, :uuid
  belongs_to :user
end

uuid is registered as ActiveUUID::Type::BinaryUUID
when I try to create these in DB

user = User.new
user.save!
device = Device.new
device.user = user
device.save!

I get a SQL error:

INSERT INTO `users` (`id`) VALUES (x'f33f3538371c4ba78715819dd972e4bf')
ActiveRecord::ValueTooLong: Mysql2::Error: Data too long for column 'user_id' at row 1: INSERT INTO `devices` (`id`, `user_id`) VALUES (x'2ae9bccb22774b35add0d8673bce282e', x'66333366333533382d333731632d346261372d383731352d383139646439373265346266')

if I decode the hex string x'66333366333533382d333731632d346261372d383731352d383139646439373265346266' I get 'f33f3538-371c-4ba7-8715-819dd972e4bf' - and it looks like id of the user

@ronaldtse
Copy link
Contributor

@alexturetsky thank you for the report!

@skalee @ribose-jeffreylau do you have time to look at this? Thanks!

@ronaldtse ronaldtse added the bug label Apr 24, 2020
@alexturetsky
Copy link
Author

@ronaldtse thanks a lot for looking into this!

@ribose-jeffreylau ribose-jeffreylau self-assigned this Apr 28, 2020
@ribose-jeffreylau
Copy link
Contributor

Hi @alexturetsky
Do you happen to have a minimal example repo that we can test on?
If not, can you try adding the following to the Device class?

  attribute :user_id, :uuid

Your feedback is appreciated!

@alexturetsky
Copy link
Author

hi @ribose-jeffreylau ,
adding attribute to the Device class helped:

irb(main):007:0> device.user = user
irb(main):008:0> device.save!
   (0.6ms)  BEGIN
  Device Create (0.6ms)  INSERT INTO `devices` (`id`,  `user_id`) VALUES (x'a8dfab4b187c459bae9da6035bfcaae8', x'd440d8c6c8444e68a8c5ff8b21ce7631')

Do you think I should update all my models with explicit attributes referencing joined tables?

@ribose-jeffreylau
Copy link
Contributor

Hi @alexturetsky ,
Yes, unlike the original activeuuid gem, all columns that have type UUID should be explicitly stated in each model.

@ronaldtse
Copy link
Contributor

Thanks @ribose-jeffreylau , can we add this as a warning on the README?

@ribose-jeffreylau
Copy link
Contributor

@ronaldtse Yes, perhaps under Adding UUIDs to models.

@alexturetsky
Copy link
Author

Thank you, all!

@ribose-jeffreylau
Copy link
Contributor

Thanks @alexturetsky !

@alexturetsky
Copy link
Author

alexturetsky commented May 13, 2020

just an fyi, interesting enough, without explicitly declaring all UUID columns, migrations ran fine (it correctly created all tables with all foreign keys and types)

CREATE TABLE `devices` (
  `id` binary(16) NOT NULL,
  `user_id` binary(16) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `users` (
  `id` binary(16) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

adding uuid attributes to the model did not break it =)

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

No branches or pull requests

3 participants