【问题标题】:Deleting table from schema - Rails从模式中删除表 - Rails
【发布时间】:2016-12-07 06:15:03
【问题描述】:

我想删除架构中的一个表。我在第一次启动项目时创建了数据库并希望删除该表。这样做的最佳方法是什么?

我尝试了rails g migration drop table :installs,但这只会创建一个空迁移?

架构:

create_table "installs", 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.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
  end

add_index "installs", ["email"], name: "index_installs_on_email", unique: true
add_index "installs", ["reset_password_token"], name: "index_installs_on_reset_password_token", unique: true

【问题讨论】:

标签: ruby-on-rails ruby rails-migrations


【解决方案1】:

如果您通过运行以下命令创建一个空迁移:
rails g migration DropInstalls
或:
rails generate migration DropInstalls

然后您可以将其添加到该空迁移中:

class DropInstalls < ActiveRecord::Migration
  def change
    drop_table :installs
  end
end

然后在命令行中运行rake db:migrate,这将删除安装表

注意:db/schema.rb 是一个自动生成的文件。要更改数据库结构,您应该使用迁移而不是尝试编辑架构文件。

【讨论】:

  • 如果它指出架构是一个自动生成的文件,因此这个答案会更完整,因此要查看的内容是错误的。
猜你喜欢
  • 2016-02-02
  • 1970-01-01
  • 1970-01-01
  • 2021-12-26
  • 1970-01-01
  • 2018-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多