簡體   English   中英

無法從 rails 應用程序中刪除 schema.rb 中的表

[英]Unable to delete tables in schema.rb from rails app

我使用 Rails 4.0 和 Ruby 2.3.0

我有一個小的 Rails 應用程序。 起初我想根據他/她注冊的內容為每個用戶分配不同的角色。 所以我從https://github.com/MartinJNash/Royce嘗試了“Royce” gem

我看到它對我的要求來說太復雜后卸載了它。

然后我嘗試了“簡單角色”gem,從這里https://github.com/stanislaw/simple_roles看看它是如何工作的。 我也卸載了tat。 當我的應用程序中有這兩個 gem 時,我運行了 db:migrate。

現在我不想在我的 schema.rb 中使用這兩個 gem 中的相應表。 它並沒有完全干擾我的應用程序,但我想刪除它們。

以下是我的 schema.rb 文件

ActiveRecord::Schema.define(version: 20160911213902) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "roles", force: :cascade do |t|
    t.string   "name"
    t.integer  "resource_id"
    t.string   "resource_type"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

add_index "roles", ["name", "resource_type", "resource_id"], name:  "index_roles_on_name_and_resource_type_and_resource_id", using: :btree

add_index "roles", ["name"], name: "index_roles_on_name", using: :btree

  create_table "royce_connector", force: :cascade do |t|
    t.integer  "roleable_id",   null: false
    t.string   "roleable_type", null: false
    t.integer  "role_id",       null: false
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "royce_connector", ["role_id"], name: "index_royce_connector_on_role_id", using: :btree
  add_index "royce_connector", ["roleable_id", "roleable_type"], name: "index_royce_connector_on_roleable_id_and_roleable_type", using: :btree

  create_table "royce_role", force: :cascade do |t|
    t.string   "name",       null: false
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "royce_role", ["name"], name: "index_royce_role_on_name", using: :btree

  create_table "sessions", force: :cascade do |t|
    t.string   "session_id", null: false
    t.text     "data"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "sessions", ["session_id"], name: "index_sessions_on_session_id", unique: true, using: :btree
  add_index "sessions", ["updated_at"], name: "index_sessions_on_updated_at", using: :btree

  create_table "users", force: :cascade do |t|
    t.string   "email"
    t.string   "name"
    t.string   "login_token"
    t.datetime "login_token_valid_until"
    t.datetime "created_at",              null: false
    t.datetime "updated_at",              null: false
  end

  create_table "users_roles", id: false, force: :cascade do |t|
    t.integer "user_id"
    t.integer "role_id"
  end

 add_index "users_roles", ["user_id", "role_id"], name: "index_users_roles_on_user_id_and_role_id", using: :btree

 create_table "vendors", force: :cascade do |t|
    t.string   "name"
    t.datetime "created_at"
    t.datetime "updated_at"
 end

  create_table "visits", force: :cascade do |t|
    t.string   "country"
    t.datetime "visited_at"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

end

即使我已經卸載了 2 個角色寶石,每次運行 rake db:migrate 時,我也會得到上述模式。

我在我的應用程序中只使用“會話”和“供應商”表。 知道我應該怎么做才能擺脫這些不需要的表格嗎?

首先檢查表是否存在......在rails控制台中......

##to view all tables 
ActiveRecord::Base.connection.tables

我使用rails console直接刪除表...

    ActiveRecord::Base.connection.execute("drop table table_name")
    ###========OR===============
    ActiveRecord::Migration.drop_table(:table_name)
   ###=========OR===============
   ActiveRecord::Migration.drop_table("table_name")     
   ###=========WITH EXAMPLE===============
   ActiveRecord::Base.connection.drop_table :subscriptions

然后,刪除遷移文件或通過更改時間戳重新命名並再次運行rake db:migrate

希望它有幫助:)

查看您的app/db/migrate目錄。 查找並刪除與這些 gem 關聯的遷移。

刪除遷移文件后,您可以再次運行rake db:migrate 然后運行rake db:schema:dump並檢查新生成的schema.rb文件以確保您沒有為您刪除的 gem 保留任何表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM