Ruby-on-rails 检查 Rails 中是否存在表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6590107/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if a table exists in Rails
提问by thenengah
I have a rake task that won't work unless a table exists. I'm working with more than 20 engineers on a website so I want to make sure they have migrated the table before they can do a rake task which will populate that respective table.
我有一个 rake 任务,除非存在表,否则它将无法工作。我在一个网站上与 20 多名工程师合作,所以我想确保他们在执行将填充相应表的 rake 任务之前已经迁移了表。
Does AR have a method such as Table.exists? How can I make sure they have migrated the table successfully?
AR 有没有类似的方法Table.exists?我如何确保他们已成功迁移表?
回答by captainpete
In Rails 5 the API became explicit regarding tables/views, collectively data sources.
在 Rails 5 中,API 变得明确了表/视图,统称为数据源。
# Tables and views
ActiveRecord::Base.connection.data_sources
ActiveRecord::Base.connection.data_source_exists? 'kittens'
# Tables
ActiveRecord::Base.connection.tables
ActiveRecord::Base.connection.table_exists? 'kittens'
# Views
ActiveRecord::Base.connection.views
ActiveRecord::Base.connection.view_exists? 'kittens'
In Rails 2, 3 & 4 the API is about tables.
在 Rails 2、3 和 4 中,API 是关于表的。
# Listing of all tables and views
ActiveRecord::Base.connection.tables
# Checks for existence of kittens table/view (Kitten model)
ActiveRecord::Base.connection.table_exists? 'kittens'
Getting the status of migrations:
获取迁移状态:
# Tells you all migrations run
ActiveRecord::Migrator.get_all_versions
# Tells you the current schema version
ActiveRecord::Migrator.current_version
If you need more APIs for migrations or metadata see:
如果您需要更多用于迁移或元数据的 API,请参阅:
- ActiveRecord::SchemaMigration
this is theActiveRecord::Baseclass for theschema_migrationstable - ActiveRecord::Migrator
where all the action happens when migrations are run
- ActiveRecord::SchemaMigration
这是表的ActiveRecord::Base类schema_migrations - ActiveRecord::Migrator
运行迁移时发生的所有操作
回答by alexey_the_cat
even if table is not exists:
即使表不存在:
model Kitten, expected table kittensrails 3:
型号Kitten,预期桌kittens轨 3:
Kitten.table_exists? #=> false
Kitten.table_exists?#=> 假
回答by kangkyu
I found this out while I was trying to remove a table via a migration:
我在尝试通过迁移删除表时发现了这一点:
drop_table :kittens if (table_exists? :kittens)
ActiveRecord::Migration.drop_table :kittens if (ActiveRecord::Base.connection.table_exists? :kittens)
works for Rails 3.2
适用于 Rails 3.2
This simpler form will become available in Rails 5:
这种更简单的形式将在 Rails 5 中可用:
drop_table :kittens, if_exists: true
Reference: https://github.com/rails/rails/pull/16366
参考:https: //github.com/rails/rails/pull/16366
And here's the Rails 5 ActiveRecord's CHANGELOG:
这是 Rails 5 ActiveRecord 的CHANGELOG:
Introduce the :if_exists option for drop_table.
Example:
drop_table(:posts, if_exists: true)That would execute:
DROP TABLE IF EXISTS postsIf the table doesn't exist, if_exists: false (the default) raises an exception whereas if_exists: true does nothing.
为 drop_table 引入 :if_exists 选项。
例子:
drop_table(:posts, if_exists: true)那将执行:
DROP TABLE IF EXISTS posts如果表不存在, if_exists: false (默认值)会引发异常,而 if_exists: true 什么也不做。
回答by Vitor Oliveira
Rails 5.1
导轨 5.1
if ActiveRecord::Base.connection.data_source_exists? 'table_name'
drop_table :table_name
end
or
或者
drop_table :table_name, if_exists: true
回答by Juan Furattini
The proper way to do this is Model.table_exists?
这样做的正确方法是 Model.table_exists?
class Dog < ApplicationRecord
# something
end
do_something if Dog.table_exists?

