1

Hi I am new to rails and I am adding a like option to my posts, however when I run rails db:migrate:redo I get the error ArgumentError: No indexes found on likes with the options provided. I am confused because I see the indexes in my schema

here is a picture of the error

enter image description here

20221215185521_create_likes.rb

class CreateLikes < ActiveRecord::Migration[7.0]
def change
create_table :likes do |t|
  t.references :user, null: false, foreign_key: true
  t.references :post, null: false, foreign_key: true

  t.timestamps
end

add_index :likes, [:user_id, :post_id], unique: true
end
end

schema.rb

ActiveRecord::Schema[7.0].define(version: 2022_12_15_185521) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "followability_relationships", force: :cascade do |t|
t.string "followerable_type", null: false
t.bigint "followerable_id", null: false
t.string "followable_type", null: false
t.bigint "followable_id", null: false
t.integer "status"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["followable_type", "followable_id"], name: 
"index_followability_relationships_on_followable"
t.index ["followerable_type", "followerable_id"], name: 
"index_followability_relationships_on_followerable"
end

create_table "likes", force: :cascade do |t|
t.bigint "user_id", null: false
t.bigint "post_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["post_id"], name: "index_likes_on_post_id"
t.index ["user_id"], name: "index_likes_on_user_id"
end

create_table "notifications", force: :cascade do |t|
t.string "recipient_type", null: false
t.bigint "recipient_id", null: false
t.string "type", null: false
t.jsonb "params"
t.datetime "read_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["read_at"], name: "index_notifications_on_read_at"
t.index ["recipient_type", "recipient_id"], name: "index_notifications_on_recipient"
end

create_table "posts", force: :cascade do |t|
t.text "body"
t.bigint "user_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["user_id"], name: "index_posts_on_user_id"
end

create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "username"
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", 
unique: true
end

add_foreign_key "likes", "posts"
add_foreign_key "likes", "users"
add_foreign_key "posts", "users"
end
CoolGuy
  • 99
  • 7
  • 3
    The error says you are trying to remove an index but the index does not exist. Why are you running `db:migrate:redo`? It seems like your schema already has the likes table (meaning the migration already ran) did you change the migration after the fact to add the unique index? If so You should not do this, instead you can create a new migration that just adds the index. – engineersmnky Dec 15 '22 at 19:20
  • 1
    agree with @engineersmnky - it looks like you have edited your migration to add the uniq qualifier to the the index. When it tries to do the rollback portion of `redo` it fails, as there is no index on post/user with a unique constraint. Instead, removing the constraint, doing the rollback. Then adding the constraint back and migrating. – trh Dec 15 '22 at 19:42

1 Answers1

0

I got this error when I tried to rake db:migrate:down VERSION=your_migrations's_version_number_here to undo some migrations.

To solve I added up and down sections to the migration, and reversed the order of the operations (so when going 'down', it would remove the index first and then the column).

Didn't work

class AddSlugToPosts < ActiveRecord::Migration[7.0]
  def change
    add_column :posts, :slug, :string
    add_index :posts, :slug, unique: true
  end
end

Did work

class AddSlugToPosts < ActiveRecord::Migration[7.0]

  # This adds the col and index in the usual way
  def up
    def change
      add_column :posts, :slug, :string
      add_index :posts, :slug, unique: true
    end
  end

  # This lets rails know how to rollback the migration
  def down
    def change
      remove_index :posts, :slug, unique: true
      remove_column :posts, :slug, :string
    end
  
  end

end
stevec
  • 41,291
  • 27
  • 223
  • 311