Tag: rails migrations

change_column_default的可逆迁移在Rails中没有任何默认值

Rails指向活动记录迁移说明你可以做到 change_column_default :products, :approved, from: true, to: false 我在Rails中有一个change方法,类似于以下内容: change_column_default :people, :height, from: nil, to: 0 意图从没有任何默认值,到默认值为零。 然而,当我尝试回滚时,我得到了 ActiveRecord::IrreversibleMigration: ActiveRecord::IrreversibleMigration 考虑到我给Rails提供了一个,为什么不接受它呢? 我正在使用Rails 4.2.0。

如何更改rails迁移文件中的主键?

我需要像这样迁移一个旧的mysql表: Products name (string, primary_key) 到这个架构: Products id (integer, primary_key, auto_generated) name (unique) 我需要在新表中填充Products.id值。 如何编写rails迁移文件? 我正在使用Rails 3.2.7 我现在有2个问题:1。我找不到在ActiveRecord :: Migration 2中删除主键的方法。我不知道如何为新添加的主键生成值。

我可以将默认值传递给rails生成迁移吗?

我想知道是否可以将默认值传递给rails g migration命令。 就像是: $ rails generate migration add_disabled_to_users disabled:boolean:false #where false is default value for disabled attribute 为了生成: class AddDisabledToUsers < ActiveRecord::Migration def change add_column :users, :disabled, :boolean, default: false end end

删除Rails应用程序中的旧迁移文件

如果架构稳定,是否允许在Rails应用程序中删除(或存档)旧的迁移文件? 我的迁移很多,我怀疑某处可能存在一些问题,因为我偶尔会在Heroku上迁移数据库时遇到问题。

如何创建迁移以仅在存在时删除索引,而不是如果不存在则抛出exception?

目前,如果books表没有created_at或updated_at字段,则当前迁移可能会失败: class AddTimestampIndexes < ActiveRecord::Migration def up remove_index :books, :created_at remove_index :books, :updated_at add_index :books, :created_at add_index :books, :updated_at end def down remove_index :books, :created_at remove_index :books, :updated_at end end remove_index是否采取任何选项以静默方式继续,如果它无法删除索引而不是引发错误?

在Rails中弹出更新后无法运行迁移

我在运行任何迁移时遇到错误: raj@notebook-pc:~/Desktop/Projects/invoicemanagement$ rails g migration RemoveDescriptionOfGoodsFromInvoiceDetails description_of_goods:string Warning: You’re using Rubygems 1.8.23 with Spring. Upgrade to at least Rubygems 2.1.0 and run `gem pristine –all` for better startup performance. /var/lib/gems/1.9.1/gems/bundler-1.9.0/lib/bundler/runtime.rb:34:in `block in setup’: You have already activated spring 1.3.3, but your Gemfile requires spring 1.3.2. Prepending `bundle exec` to your command may solve this. (Gem::LoadError) […]

Rails 3.1:无法在添加它的同一个迁移中写入列

我有一个可以正常运行的add_column迁移。 但是,在运行它并启动控制台后,我会发现first_name和last_name列完全为空。 我试过用save! 相反,它具有相同的效果 – 没有报告错误。 这是原始的: class UserAddFirstNameAndLastName < ActiveRecord::Migration def change # add column first name, last name string add_column :users, :first_name, :string add_column :users, :last_name, :string User.all.each do |u| u.first_name = 'first name' u.last_name = 'last name' u.save end end end 我还认为这可能是一些类加载问题,所以我插入了User行来强制用户类在循环之前重新加载。 没有骰子。 当我将其拆分为两次迁移时,实现了期望的效果。 有人对此有解释吗? 我发誓我甚至在过去迁移的同一个项目中做过这个。 其他说明:设计用户引擎,在运行迁移之前将新列添加到User类中的attr_accessible 。

管理mongoid迁移

有人能给我一个简短的介绍,使用Mongoid在Rails中进行数据库迁移吗? 我对每个文档迁移的懒惰特别感兴趣。 这意味着,无论何时从数据库中读取文档,都可以将其迁移到最新版本并再次保存。 有没有人以前做过这种事情? 我遇到过mongoid_rails_migrations ,但它没有提供任何类型的文档,虽然它看起来像这样做,但我不确定如何使用它。 我应该指出,我只是在概念上熟悉ActiveRecord迁移。

heroku运行rake db:迁移错误

我想在我的应用程序上运行我在heroku上的迁移,但是我收到此错误: Running `rake db:migrate` attached to terminal… up, run.1 DEPRECATION WARNING: You have Rails 2.3-style plugins in vendor/plugins! Support for these plugins will be removed in Rails 4.0. Move them out and bundle them in your Gemfile, or fold them in to your app as lib/myplugin/* and config/initializers/myplugin.rb. See the release notes for more on […]

将rails应用程序“连接”到已存在的MySQL数据库?

所以在我的公司,我们正在慢慢转向Rails而不是PHP(准确地说是Code Igniter)。 所以,我们实际的PHP应用程序使用的是Mysql数据库,我想将新的Rails应用程序连接到此数据库,但同时我们的PHP仍在运行,因此我无法更改数据库。 我真的不知道应该从哪里开始使用所有导轨function(或者至少尽可能多)。