以下是否正确?
change_column :tablename, :fieldname, :limit => null
Run Code Online (Sandbox Code Playgroud) 我有一个删除列的迁移:
def change
remove_column :foos, :bar, :boolean
end
Run Code Online (Sandbox Code Playgroud)
当我尝试rake db:rollback迁移时,我收到以下错误:
remove_column is only reversible if given a type.
Run Code Online (Sandbox Code Playgroud)
该ActiveRecord的::迁移文件说,下面是签名remove_column:
remove_column(table_name, column_name, type, options)
Run Code Online (Sandbox Code Playgroud)
所以我在这种情况下的类型应该是:boolean,并且我希望迁移是可逆的.我错过了什么?
我当然可以打破这一点为up和down迁移来避免这个问题,但我想明白为什么change语法是不是在这种情况下工作.
我一直在做这里发现的Rails教程,并且已经成功到必须使用$ rails db:migrate迁移 Comments 迁移.在此之前,我已经能够生成文章模型并迁移文章创建迁移而没有任何问题.在这两次迁移之间,我的Gemfile中没有任何变化,所以我不确定Bundler有什么问题.
以下是错误,然后是完整的命令行输出,以及我的Gemfile和schema.rb:
Gem::LoadError: can't activate pg (~> 0.18), already activated pg-1.0.0.
Gem::LoadError: Specified 'postgresql' for database adapter, but the gem is not loaded. Add `gem 'pg'` to your Gemfile (and ensure its version is at the minimum required by ActiveRecord).
Run Code Online (Sandbox Code Playgroud)
完整的命令行输出
xxx:gangelo: ~/dev/rails/test/blog (master*) ? rbenv exec rails db:migrate
rails aborted!
Gem::LoadError: Specified 'postgresql' for database adapter, but the gem is not loaded. Add `gem 'pg'` to your Gemfile (and …Run Code Online (Sandbox Code Playgroud) 我有以下迁移,我希望能够检查与环境相关的当前数据库是否是一个mysql数据库.如果是mysql,那么我想执行特定于数据库的SQL.
我该怎么做?
class AddUsersFb < ActiveRecord::Migration
def self.up
add_column :users, :fb_user_id, :integer
add_column :users, :email_hash, :string
#if mysql
#execute("alter table users modify fb_user_id bigint")
end
def self.down
remove_column :users, :fb_user_id
remove_column :users, :email_hash
end
end
创建迁移文件后,rails generate migration AddClientToUser我可以编辑我的迁移文件,如下所示:
class AddClientToUser < ActiveRecord::Migration
def self.up
change_table :users do |t|
t.references :client
end
end
def self.down
change_table :users do |t|
t.remove :client_id
end
end
end
Run Code Online (Sandbox Code Playgroud)
这是反转迁移中添加的参考列的正确方法吗?
鉴于以下内容schema.rb:
create_table "people", force: true do |t|
t.string "name", null: false
t.integer "age"
t.integer "height"
t.string "email"
t.boolean "married", default: false
t.text "bio"
t.integer "fav_number"
t.decimal "lucky_num", precision: 2, scale: 2
t.datetime "birthday"
t.datetime "created_at"
t.datetime "updated_at"
end
Run Code Online (Sandbox Code Playgroud)
我想删除name默认值null: false.我已尝试使用单独的迁移change_column_default,但这对其没有影响schema.rb.有什么建议?
我似乎无法找到允许我跳过迁移的选项或任何内容.
我知道你在想什么:"你永远不应该那样做......"
我需要跳过一个迁移,它对我的开发数据库中不存在的特定用户记录进行更改.我不想更改迁移,因为它不是我应该使用的源代码的一部分.有没有办法跳过迁移或跳过失败的迁移?
提前致谢!
考虑到性能改进,我想知道是否以及哪些索引对连接表有用(特别是在Rails 3 has_and_belongs_to_many上下文中使用).
我的模型是Foo和Bar每个rails惯例,我有一个名为的连接表bars_foos.没有主键或时间戳使老油田在该表bar_id:integer和foo_id:integer.我有兴趣知道以下哪个索引是最好的,没有重复:
add_index :bars_foos, [:bar_id, :foo_id]add_index :bars_foos, :bar_idadd_index :bars_foos, :foo_id基本上,我不确定复合索引是否足够,假设它有助于开始.我相信复合索引可以用作第一项的单个索引,这就是为什么我很确定使用所有三行肯定会导致不必要的重复.
最常见的用法将给出一个模型实例Foo,我将要求bars使用RoR语法关联它foo.bars,反之亦然,bar.foos用于模型的实例Bar.
这些将产生的类型的查询SELECT * FROM bars_foos WHERE foo_id = ?和SELECT * FROM bars_foos WHERE bar_id = ?分别然后使用这些所得ID来SELECT * FROM bars WHERE ID in (?)和SELECT * FROM foos WHERE ID in …
这是我在rails 3.2.2中的迁移:
class CreateStatistics < ActiveRecord::Migration
def change
create_table :statistics do |t|
t.string :name
t.integer :item_id
t.integer :value
t.text :desc
t.timestamps
t.index [:name, :item_id]
end
end
end
Run Code Online (Sandbox Code Playgroud)
这是迁移错误:
== CreateStatistics: migrating ===============================================
-- create_table(:statistics)
ActiveRecord::ConnectionAdapters::TableDefinition
rake aborted!
An error has occurred, all later migrations canceled:
undefined method `index' for #<ActiveRecord::ConnectionAdapters::TableDefinition:0xbd16888>
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
Run Code Online (Sandbox Code Playgroud)
什么是创建索引的正确方法?
我创建了一个包含"note"列的新表.默认值是varchar(255)我相信但是我希望这个列是文本区域而不是字段并允许更多数据.我想我会在ActiveRecord :: Migration文件中进行此更改,但我对格式感到好奇.我只是将varchar(255)更改为varchar(1000)吗?(如果是这样的格式是什么?
def self.up
create_table :notes do |t|
t.string :note :varchar(1000)
end
Run Code Online (Sandbox Code Playgroud)
这是正确的格式吗?此外,如何将输入字段设置为多行.对不起,如果这很容易,但我是编程和RoR的新手.谢谢.
rails-migrations ×10
ruby ×4
migration ×2
activerecord ×1
bundler ×1
indexing ×1
postgresql ×1
rake ×1