0
votes

My Error

== 20150727183532 ActsAsTaggableOnMigration: migrating ======================== -- create_table(:tags) rake aborted! StandardError: An error has occurred, this and all later migrations canceled:

PG::DuplicateTable: ERROR: relation "tags" already exists : CREATE TABLE "tags" ("id" serial primary key, "name" character varying)

Second Migration in Database:

    create_table :tags do |t|
        t.string            :name
        t.integer           :taggings_count, default: 0
    end
    add_index :tags, :name, unique: true


    create_table :taggings do |t|
        t.references        :tag
        t.references        :taggable,      polymorphic: true
        t.references        :tagger,        polymorphic: true
        t.string                :context,       limit: 128
        t.datetime          :created_at
    end
    add_index :taggings, [ :tag_id, :taggable_id, :taggable_type, :context, :tagger_id, :tagger_type ],
        unique: true, name: 'taggings_idx'

end

This is a later migration where the problem appears to be coming from?

def self.up

create_table :tags do |t|
  t.string :name
end

create_table :taggings do |t|
  t.references :tag

  # 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

  Limit is created to prevent MySQL error on index
  # length for MyISAM table type: http://bit.ly/vgW2Ql
  t.string :context, limit: 128

  t.datetime :created_at
end

add_index :taggings, :tag_id
add_index :taggings, [:taggable_id, :taggable_type, :context]

end

def self.down drop_table :taggings drop_table :tags end

I am trying to understand what the person that made this migration was intending to do.

3

3 Answers

1
votes

This can't be intentional, because it can't work. As you've seen, you can't create a table twice. You should delete one of the migrations, possibly merging from the one you delete into the other one.

The only differences are the taggings_count field and the indexes. There isn't enough to go on here to say whether you need taggings_count or which is the better index. If I had to guess, I'd say the index on the first was trying to create a covering index, for what that's worth.

0
votes

Get use to it. This error will happen often in the future. Usually, it happens when your migration failed in the middle, and for example your table was created, but later creation of indexes failed. Then when you are trying to rerun the migration the table already exists, and migration fails.

There are several ways to deal with such situation: 1) you can drop the table or anything else that was partially created, and rerun. 2) you can edit that particular migration and comment out the table creation part while rerunning (then you can uncomment it).

I personally prefer the #2.

I had to say that this situation happens only for some databases. You will see it with MySQL, but will not see it with PostreSQL. It happens because PostreSQL fully rolls back changes for failed migration (including successfully created tables and such); and MySQL decides that changes in partially successful migration should not be rolled back.

0
votes

ActsAsTaggable* will sometimes install a duplicate migration (or near duplicate) when upgrading the gem. Is the db/schema.rb checked in to the repo? If so, the unchanged version (once you start migrating, it gets changed) will have the "right" settings and help you figure out which one to remove.