activeuuid icon indicating copy to clipboard operation
activeuuid copied to clipboard

using activeuuid with sqlite

Open Raven24 opened this issue 12 years ago • 7 comments

Hi!

I am currently trying to get this gem to work with sqlite so that it can be used in a default rails development environment. I have already worked on some issues that prevented a new entry from being saved and now I am having issues retrieving data from the database by its uuid. This is what I am currently using:

> Person.find(UUIDTools::UUID.parse("0320cdfa-04d2-11e1-a1a1-14dae903e06a"))
/home/florian/.rvm/gems/ruby-1.8.7-p352@contacts/gems/activerecord-3.1.1/lib/active_record/connection_adapters/abstract/quoting.rb:46: warning: Object#id will be deprecated; use Object#object_id
  Person Load (0.2ms)  SELECT "people".* FROM "people" WHERE "people"."id" = ? LIMIT 1  [["id", #<UUID:0x3fbf87905e1c UUID:0320cdfa-04d2-11e1-a1a1-14dae903e06a>]]
ActiveRecord::RecordNotFound: Couldn't find Person with id=0320cdfa-04d2-11e1-a1a1-14dae903e06a
        from /home/florian/.rvm/gems/ruby-1.8.7-p352@contacts/gems/activerecord-3.1.1/lib/active_record/relation/finder_methods.rb:339:in `find_one'
        from /home/florian/.rvm/gems/ruby-1.8.7-p352@contacts/gems/activerecord-3.1.1/lib/active_record/relation/finder_methods.rb:310:in `find_with_ids'
        from /home/florian/.rvm/gems/ruby-1.8.7-p352@contacts/gems/activerecord-3.1.1/lib/active_record/relation/finder_methods.rb:107:in `find'
        from /home/florian/.rvm/gems/ruby-1.8.7-p352@contacts/gems/activerecord-3.1.1/lib/active_record/base.rb:441:in `__send__'
        from /home/florian/.rvm/gems/ruby-1.8.7-p352@contacts/gems/activerecord-3.1.1/lib/active_record/base.rb:441:in `find'
        from (irb):1

From my tests I can say that the Arel::Visitor is never used and it looks like ActiveRecord is trying to use the to_s method of the UUID object. This, of course, is then not the right way to retrieve an entry...

Maybe someone can help me with that ... I will put a fork on github a little later.

Edit: Just FYI, here is my fork: https://github.com/Raven24/activeuuid

Raven24 avatar Nov 02 '11 20:11 Raven24

With the code I have in my fork in the 'sqlite' branch I am now able to create and find models that are using uuids as id. Unfortunately since the sqlite connection adapter seems to prepare the queries differently than the mysql adapter, querying rows only works like this:

Person.where( :id => UUIDTools::UUID.parse("0320cdfa-04d2-11e1-a1a1-14dae903e06a") )

instead of

Person.find(UUIDTools::UUID.parse("0320cdfa-04d2-11e1-a1a1-14dae903e06a"))

The first one uses the Arel::Visitor, the other one seems to just call the to_s method on the uuid object, although I am not entirely sure on that.

I would still very much appreciate any help on this.

Edit: This also works:

Person.find_by_id(UUIDTools::UUID.parse("0320cdfa-04d2-11e1-a1a1-14dae903e06a"))

Raven24 avatar Nov 15 '11 16:11 Raven24

A little update on my work: I think I really got something going there. It is now even possible to do nested forms and everything seems to work on sqlite just as on mysql. (Still some testing required) If you want to take a look, check out the sqlite branch in my fork. (link in the first comment)

Raven24 avatar Jan 27 '12 15:01 Raven24

SQLite3::ConstraintException: PRIMARY KEY must be unique: , so how?? what happened about uuid??

slamet avatar Jan 19 '13 10:01 slamet

What do you mean? Activeuuid is evolving. For example, now you can do Person.find("0320cdfa-04d2-11e1-a1a1-14dae903e06a"). UUID fields now acts as native AR types. So, what is wrong? Describe your problem.

pyromaniac avatar Jan 19 '13 10:01 pyromaniac

@pyromaniac Person.find("0320cdfa-04d2-11e1-a1a1-14dae903e06a") dose not work with the current released version. I have tryed it withe mysql and sqlite.

agirorn avatar Aug 29 '14 10:08 agirorn

Could you please post an exception and more info?

pyromaniac avatar Aug 29 '14 10:08 pyromaniac

db/migrate/20140817181933_create_products.rb

class CreateProducts < ActiveRecord::Migration
  def change
    create_table :products, :id => false do |t|
      t.uuid :id, :primary_key => true
      t.string :name
      t.timestamps
    end
  end
end

app/models/product.rb

class Product < ActiveRecord::Base
  include ActiveUUID::UUID
end

rails c

2.1.1 :015 > product = Product.create!
   (0.2ms)  BEGIN
  SQL (0.3ms)  INSERT INTO `products` (`created_at`, `id`, `updated_at`) VALUES ('2014-08-29 15:02:21', x'7c4664759d1649979f665cb94dbcb347', '2014-08-29 15:02:21')
   (7.1ms)  COMMIT
 => #<Product id: #<UUID:0x8190bd94 UUID:7c466475-9d16-4997-9f66-5cb94dbcb347>, name: nil, price: nil, description: nil, url: nil, company_id: nil, created_at: "2014-08-29 15:02:21", updated_at: "2014-08-29 15:02:21">
2.1.1 :016 > Product.find product.id.to_s
  Product Load (0.5ms)  SELECT  `products`.* FROM `products`  WHERE `products`.`id` = '7c466475-9d16-4997-9f66-5cb94dbcb347' LIMIT 1
ActiveRecord::RecordNotFound: Couldn't find Product with 'id'=7c466475-9d16-4997-9f66-5cb94dbcb347
        from /Users/agir/.rvm/gems/[email protected]/gems/activerecord-4.1.4/lib/active_record/relation/finder_methods.rb:320:in `raise_record_not_found_exception!'
        from /Users/agir/.rvm/gems/[email protected]/gems/activerecord-4.1.4/lib/active_record/relation/finder_methods.rb:420:in `find_one'
        from /Users/agir/.rvm/gems/[email protected]/gems/activerecord-4.1.4/lib/active_record/relation/finder_methods.rb:404:in `find_with_ids'
        from /Users/agir/.rvm/gems/[email protected]/gems/activerecord-4.1.4/lib/active_record/relation/finder_methods.rb:68:in `find'
        from /Users/agir/.rvm/gems/[email protected]/gems/activerecord-4.1.4/lib/active_record/querying.rb:3:in `find'
        from (irb):16
        from /Users/agir/.rvm/gems/[email protected]/gems/railties-4.1.4/lib/rails/commands/console.rb:90:in `start'
        from /Users/agir/.rvm/gems/[email protected]/gems/railties-4.1.4/lib/rails/commands/console.rb:9:in `start'
        from /Users/agir/.rvm/gems/[email protected]/gems/railties-4.1.4/lib/rails/commands/commands_tasks.rb:69:in `console'
        from /Users/agir/.rvm/gems/[email protected]/gems/railties-4.1.4/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
        from /Users/agir/.rvm/gems/[email protected]/gems/railties-4.1.4/lib/rails/commands.rb:17:in `<top (required)>'
        from bin/rails:4:in `require'
        from bin/rails:4:in `<main>'

agirorn avatar Aug 29 '14 15:08 agirorn