我添加了一个我认为我会需要的表,但现在不再打算使用它。我应该如何删除该表?
我已经运行了迁移,所以该表在我的数据库中。我rails generate migration
认为应该能够处理这个问题,但我还没有弄清楚怎么做。
我试过了:
rails generate migration drop_tablename
但这只是产生了一个空迁移。
在 Rails 中删除表格的“官方”方式是什么?
我添加了一个我认为我会需要的表,但现在不再打算使用它。我应该如何删除该表?
我已经运行了迁移,所以该表在我的数据库中。我rails generate migration
认为应该能够处理这个问题,但我还没有弄清楚怎么做。
我试过了:
rails generate migration drop_tablename
但这只是产生了一个空迁移。
在 Rails 中删除表格的“官方”方式是什么?
您并不总是能够简单地生成迁移以拥有您想要的代码。您可以创建一个空迁移,然后使用您需要的代码填充它。
您可以在此处找到有关如何在迁移中完成不同任务的信息:
http://api.rubyonrails.org/classes/ActiveRecord/Migration.html
更具体地说,您可以看到如何使用以下方法删除表:
drop_table :table_name
首先使用您想要的任何名称生成一个空迁移。这样做很重要,因为它会创建适当的日期。
rails generate migration DropProductsTable
这将在 /db/migrate/ 中生成一个 .rb 文件,如 20111015185025_drop_products_table.rb
现在将该文件编辑为如下所示:
class DropProductsTable < ActiveRecord::Migration
def up
drop_table :products
end
def down
raise ActiveRecord::IrreversibleMigration
end
end
我唯一添加的是drop_table :products
and raise ActiveRecord::IrreversibleMigration
。
然后运行rake db:migrate
,它会为你放下桌子。
手动编写迁移。例如运行rails g migration DropUsers
。
至于迁移的代码,我只是引用 Maxwell Holder 的帖子Rails Migration Checklist
rake db:migrate
然后rake db:rollback
会失败class DropUsers < ActiveRecord::Migration
def change
drop_table :users
end
end
class DropUsers < ActiveRecord::Migration
def up
drop_table :users
end
def down
fail ActiveRecord::IrreversibleMigration
end
end
class DropUsers < ActiveRecord::Migration
def change
drop_table :users do |t|
t.string :email, null: false
t.timestamps null: false
end
end
end
您需要使用以下命令创建一个新的迁移文件
rails generate migration drop_table_xyz
并在新生成的迁移文件 (db/migration/xxxxxxx_drop_table_xyz) 中写入 drop_table 代码,例如
drop_table :tablename
或者,如果您想在不迁移的情况下删除表,只需通过以下方式打开 rails 控制台
$ rails c
并执行以下命令
ActiveRecord::Base.connection.execute("drop table table_name")
或者您可以使用更简化的命令
ActiveRecord::Migration.drop_table(:table_name)
class DropUsers < ActiveRecord::Migration
def change
drop_table :users do |t|
t.string :name
t.timestamps
end
end
end
简单而正式的方法是这样的:
rails g migration drop_tablename
现在转到您的 db/migrate 并查找包含 drop_tablename 作为文件名的文件并将其编辑为此。
def change
drop_table :table_name
end
然后你需要运行
rake db:migrate
在您的控制台上。
我无法使其与迁移脚本一起使用,因此我继续使用此解决方案。使用终端进入 rails 控制台:
rails c
类型
ActiveRecord::Migration.drop_table(:tablename)
这对我来说很有用。这将删除以前的表。别忘了跑
rails db:migrate
我认为,要完全“官方”,您需要创建一个新的迁移,并将 drop_table 放在 self.up 中。然后 self.down 方法应包含完整重新创建表的所有代码。据推测,该代码可能只是在您创建迁移时从 schema.rb 中获取的。
输入代码来创建一个您知道不再需要的表似乎有点奇怪,但这会使所有迁移代码保持完整和“官方”,对吗?
我只是为了一张我需要放下的桌子这样做,但老实说没有测试“向下”并且不知道为什么我会这样做。
引发异常或尝试重新创建现在为空的表的替代方法 - 同时仍启用迁移回滚、重做等 -
def change
drop_table(:users, force: true) if ActiveRecord::Base.connection.tables.include?('users')
end
您可以简单地从 rails 控制台删除一个表。首先打开控制台
$ rails c
然后将此命令粘贴到控制台
ActiveRecord::Migration.drop_table(:table_name)
将table_name替换为要删除的表。
您也可以直接从终端删除表。只需进入应用程序的根目录并运行此命令
$ rails runner "Util::Table.clobber 'table_name'"
您可以按照指南中的方式回滚迁移:
http://guides.rubyonrails.org/active_record_migrations.html#reverting-previous-migrations
生成迁移:
rails generate migration revert_create_tablename
编写迁移:
require_relative '20121212123456_create_tablename'
class RevertCreateTablename < ActiveRecord::Migration[5.0]
def change
revert CreateTablename
end
end
通过这种方式,您还可以回滚并用于恢复任何迁移
打开你的 Rails 控制台
ActiveRecord::Base.connection.execute("drop table table_name")
ActiveRecord::Base.connection.drop_table :table_name
如果有人正在寻找如何在 SQL 中执行此操作。
rails dbconsole
从终端输入
输入密码
在控制台做
USE db_name;
DROP TABLE table_name;
exit
请不要忘记从架构中删除迁移文件和表结构
我需要删除我们的迁移脚本以及表本身......
class Util::Table < ActiveRecord::Migration
def self.clobber(table_name)
# drop the table
if ActiveRecord::Base.connection.table_exists? table_name
puts "\n== " + table_name.upcase.cyan + " ! "
<< Time.now.strftime("%H:%M:%S").yellow
drop_table table_name
end
# locate any existing migrations for a table and delete them
base_folder = File.join(Rails.root.to_s, 'db', 'migrate')
Dir[File.join(base_folder, '**', '*.rb')].each do |file|
if file =~ /create_#{table_name}.rb/
puts "== deleting migration: " + file.cyan + " ! "
<< Time.now.strftime("%H:%M:%S").yellow
FileUtils.rm_rf(file)
break
end
end
end
def self.clobber_all
# delete every table in the db, along with every corresponding migration
ActiveRecord::Base.connection.tables.each {|t| clobber t}
end
end
从终端窗口运行:
$ rails runner "Util::Table.clobber 'your_table_name'"
或者
$ rails runner "Util::Table.clobber_all"
您不能简单地运行drop_table :table_name
,而是可以通过运行创建一个空迁移:
rails g migration DropInstalls
然后,您可以将其添加到该空迁移中:
class DropInstalls < ActiveRecord::Migration
def change
drop_table :installs
end
end
然后rails db:migrate
在应该删除安装表的命令行中运行解决方案在这里找到
跑
rake db:migrate:down VERSION=<version>
<version>
您要还原的迁移文件的版本号在哪里。
例子:-
rake db:migrate:down VERSION=3846656238
在迁移中,您可以通过以下方式删除表:
drop_table(table_name, **options)
选项:
:force
设置为 :cascade 也可以删除依赖对象。默认为假
:if_exists
设置为 true 以仅在表存在时删除表。默认为假
例子:
为删除表创建迁移,例如我们要删除User
表
rails g migration DropUsers
Running via Spring preloader in process 13189
invoke active_record
create db/migrate/20211110174028_drop_users.rb
编辑迁移文件,在我们的例子中是db/migrate/20211110174028_drop_users.rb
class DropUsers < ActiveRecord::Migration[6.1]
def change
drop_table :users, if_exist: true
end
end
运行迁移以删除User
表
rails db:migrate
== 20211110174028 DropUsers: migrating ===============================
-- drop_table(:users, {:if_exist=>true})
-> 0.4607s
你能做的最好的方法是
rails g migration Drop_table_Users
然后执行以下操作
rake db:migrate
删除表/迁移
运行:- $ rails 生成迁移 DropTablename
exp:- $ rails 生成迁移 DropProducts
如果你想删除一个特定的表,你可以做
$ rails db:migrate:up VERSION=[Here you can insert timestamp of table]
否则,如果您想删除所有数据库,您可以这样做
$rails db:drop
运行此命令:-
rails g migration drop_table_name
然后:
rake db:migrate
或者如果您使用的是 MySql 数据库,那么:
show databases;
show tables;
drop table_name;
如果要从架构中删除表,请执行以下操作 -
rails db:rollback