【问题标题】:schema.rb file is not updatingschema.rb 文件未更新
【发布时间】:2015-07-02 11:45:30
【问题描述】:

在我的应用中,我编辑了此处显示的迁移文件:

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :first_name
      t.string :last_name
      t.string :dj_alias
      t.boolean :site_admin
      t.integer :station_id
      t.string :byline
      t.string :bio

      t.timestamps null: false
    end
  end
end

拥有 bio 和 byline 字段。但是,当我运行 rake db:reset 时,schema.rb 文件没有任何变化。我看到的唯一错误是这个代码块:

ActiveRecord::Base.connection.tables.each do |table|
  result = ActiveRecord::Base.connection.execute("SELECT id FROM #{table} ORDER BY id DESC LIMIT 1") rescue ( puts "Warning: not procesing table #{table}. Id is missing?" ; next )
  ai_val = result.any? ? result.first['id'].to_i + 1 : 1
  puts "Resetting auto increment ID for #{table} to #{ai_val}"

  ActiveRecord::Base.connection.execute("ALTER SEQUENCE #{table}_id_seq  RESTART WITH #{ai_val}")
end

位于seeds.rb 文件的底部,用于处理种子文件的索引。当我运行 rake db:reset 时,第一行中的救援语句显示:警告:未处理表 schema_migrations。身份证不见了?

我想我很困惑为什么这个声明正在拯救这个?虽然看起来可能是原因,但是schema.rb重置不是在访问seeds.rb文件之前发生的吗?

这是 rake db:migrate:status 的输出

 Status   Migration ID    Migration Name
--------------------------------------------------
up     20150225041954  Create songs
up     20150225042739  Create albums
up     20150225043102  Create artists
up     20150225043854  Create playlists
up     20150225044118  Create users
up     20150225044314  Create stations
up     20150225061259  Create featured artists
up     20150225153938  Add devise to users
up     20150225200646  Create reviews
up     20150321171830  Stations users
up     20150323200255  Add last fm to album
up     20150323200432  Add last fm to artist
up     20150323200513  Add last fm to song
up     20150325052314  Albums stations
up     20150325061241  Playlist songs
up     20150327172516  Add image url to albums
up     20150327172532  Add image url to artists

【问题讨论】:

    标签: ruby-on-rails postgresql rake


    【解决方案1】:

    要在迁移文件中应用新的更改,您需要运行 rake db:migrate。如果在您进行更改之前已运行迁移,则运行 rake db:rollback 以回滚迁移并再次应用它。

    rake db:reset 不会在迁移文件中应用新的更改。它会尝试加载 schema.rb 文件中已有的内容。

    请参阅http://edgeguides.rubyonrails.org/active_record_migrations.html#setup-the-databasehttp://edgeguides.rubyonrails.org/active_record_migrations.html#resetting-the-database,了解有关rake db:reset 工作原理的更多详细信息。

    运行 rake db:migrate:status 以查看正在运行的迁移。

    【讨论】:

    • 我确实尝试过,是否可能需要多次运行 rake db:rollback?
    • 我在 rake db:migrate:status 的输出中编辑到我原来​​的问题中
    • 查看stackoverflow.com/a/6635407/429758 了解如何回滚特定迁移。
    猜你喜欢
    • 1970-01-01
    • 2013-02-05
    • 1970-01-01
    • 2013-12-27
    • 1970-01-01
    • 2011-07-10
    • 2013-10-06
    • 2014-09-04
    • 1970-01-01
    相关资源
    最近更新 更多