mbleigh / acts-as-taggable-on

A tagging plugin for Rails applications that allows for custom tagging along dynamic contexts.
http://mbleigh.lighthouseapp.com/projects/10116-acts-as-taggable-on
MIT License
4.98k stars 1.19k forks source link

Generated migration needs edit to work with uuids. #698

Open david-crowell opened 9 years ago

david-crowell commented 9 years ago

If the tables you're making taggable use uuids rather than integer ids, then in in ActsAsTaggableOnMigration you need to change

t.references :taggable, polymorphic: true

to

t.references :taggable, polymorphic: true, type: :uuid

Otherwise it fails in hilariously non-deterministic ways.

lmmendes commented 7 years ago

I my case I needed to define the tagger also as an uuid type field since I'm using owned tags and the owner (the tagger) has also his primary id key defined as uuid

      t.references :taggable, polymorphic: true, type: :uuid
      t.references :tagger, polymorphic: true, type: :uuid
bobbytables commented 6 years ago

For others using rails 5.2 and are trying to use UUIDs for primary keys, here's what I had to do:

 # This migration comes from acts_as_taggable_on_engine (originally 1)
 if ActiveRecord.gem_version >= Gem::Version.new('5.0')
-  class ActsAsTaggableOnMigration < ActiveRecord::Migration[4.2]; end
+  class ActsAsTaggableOnMigration < ActiveRecord::Migration[5.2]; end
 else
   class ActsAsTaggableOnMigration < ActiveRecord::Migration; end
 end
 ActsAsTaggableOnMigration.class_eval do
   def self.up
-    create_table :tags do |t|
+    create_table :tags, id: :uuid do |t|
       t.string :name
     end

-    create_table :taggings do |t|
+    create_table :taggings, id: :uuid do |t|
-      t.references :tag
+      t.references :tag, type: :uuid

       # You should make sure that the column created is
       # long enough to store the required class names.
-      t.references :taggable, polymorphic: true
-      t.references :tagger, polymorphic: true
+      t.references :taggable, polymorphic: true, type: :uuid
+      t.references :tagger, polymorphic: true, type: :uuid

       # Limit is created to prevent MySQL error on index
       # length for MyISAM table type: http://bit.ly/vgW2Ql
@@ -25,7 +25,6 @@ ActsAsTaggableOnMigration.class_eval do
       t.datetime :created_at
     end

-    add_index :taggings, :tag_id
     add_index :taggings, [:taggable_id, :taggable_type, :context]
   end
cthorner commented 5 years ago

Here is what I did for 4.2: In addition to the other changes(although I didn't remove the :taggings, :tag_id index): I added uuid with this:

create_table :tags, id: false do |t|
      t.uuid :id, null: false, default: 'uuid_generate_v4()', primary_key: true
      t.string :name
    end

    create_table :taggings, id: false do |t|
      t.uuid :id, null: false, default: 'uuid_generate_v4()', primary_key: true
      t.references :tag, type: :uuid