ruby-on-rails 参数错误:未找到包含所提供选项的赞的索引

1l5u6lss  于 2023-02-26  发布在  Ruby
关注(0)|答案(1)|浏览(119)

大家好,我是Rails的新手,我在我的帖子中添加了一个类似的选项,但是当我运行rails db:migrate:redo时,我得到了错误ArgumentError:未找到带有所提供选项的赞的索引。我很困惑,因为我在架构中看到了索引
以下是错误的图片

20221215185521_创建_喜欢. 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
vulvrdjw

vulvrdjw1#

当我尝试使用rake db:migrate:down VERSION=your_migrations's_version_number_here撤消某些迁移时,出现此错误。
为了解决这个问题,我在迁移中添加了updown部分,并颠倒了操作的顺序(因此,当“向下”时,它将首先删除索引,然后删除列)。

不起作用

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

有效

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

相关问题