jashmenn / activeuuid

Binary uuid keys in Rails
MIT License
340 stars 124 forks source link

Normal that my primary key is "MUL"? #25

Open 9mm opened 11 years ago

9mm commented 11 years ago

I'm looking at my MySQL table after I set :primary_key => true, and it's saying instead of PRI it's "MUL". Is this okay?

9mm commented 11 years ago

Also, How do I make it a v1 UUID instead of v4?

pyromaniac commented 11 years ago

Regarding the first question - please, show your sources. The second - try to redefine https://github.com/jashmenn/activeuuid/blob/master/lib/activeuuid/uuid.rb#L119 at your model.

9mm commented 11 years ago

Migration:

class CreateSurveys < ActiveRecord::Migration
  def change
    create_table :surveys, :id => false do |t|
      t.references :employee, :null => false
      t.uuid :id, :primary_key => true
      t.datetime :expired_at
      t.timestamps
    end
    add_index :surveys, :id
  end
end       

survey.rb

class Survey < ActiveRecord::Base

  include ActiveUUID::UUID

  belongs_to :employee

  validates :employee, :presence => true  

end
pyromaniac commented 11 years ago

Looks proper, except add_index :surveys, :id, primary keys in sql are indexed by default. Still didn't get you. What is "MUL" then? Where did you see this?

pyromaniac commented 11 years ago

Ah, got it. It is MUL because of add_index :surveys, :id. Just remove this line and it will become PRI

9mm commented 11 years ago

That fixed it! Thanks! I saw the add_index line right on the documentation, as well as a few guides online. I don't know why everyone has that in their examples if it's not supposed to be on a primary key... hmm.

I'm sure you are busy but would you mind answering a few straggling questions of mine, just for the sake of learning?

  1. Do you think its important I try to override the v4 to be v1? I read on the issues here that v4 isn't as secure. The purpose of me using a UUID is because each hour I need to email surveys out where a user will click a link. I don't want the link in the email to be sequentially numbered as a normal key so people cant find out other ID's. I will also be looking up surveys by UUID so that is important as well.
  2. Is there any particular way I can remove dashes (for the URL). I'd rather not have them when people see the URL. Currently I'm just using the code below. I'm not sure if it's acceptable to use that, and then to convert back, maybe add the dashes back in the correct spot. Or.. if there is something magic about this where my assumption would be foolish.
def token                                
  id.hexdigest                           
end                                      

def self.id_from_token(token)            
  UUIDTools::UUID.parse_hexdigest(token) 
end                                      

Thanks!

pyromaniac commented 11 years ago

If you ask me - there is no indication to use activeuuid at all. In your place, I would make a usual autoincrement id and additional token field with uniq index to store hex values. And use simply before_save to generate this token.

I do not know about the difference between v1 and v4, but you can use whatever you need with http://www.ruby-doc.org/stdlib-1.9.3/libdoc/securerandom/rdoc/SecureRandom.html