【问题标题】:Ruby Rails rake db migrate not working or schema file not updatingRuby Rails rake db migrate 不工作或模式文件不更新
【发布时间】:2014-01-26 00:58:09
【问题描述】:

所以我在 schema.rb 上创建了一个新表 这是我放的新代码行:

  create_table "book", :force => true do |t|
    t.integer  "user_id"
    t.string   "title"
    t.integer   "count"
  end

但是当我运行 rake db:migrate 命令 schema.rb 变成旧文件后,当我打开它时,我的新 create_table 代码没有任何想法?我是 ruby​​ rails 新手 谢谢!

我也试过这个:

rake db:drop:all
rake db:create:all
rake db:migrate

但 schema.rb 文件仍然没有更新到新的。

【问题讨论】:

    标签: ruby-on-rails ruby


    【解决方案1】:

    schema.rb 文件已生成,您在其中所做的任何更改都将在下次生成时消失。在 Rails 中进行数据库更改的常规方法是使用 migrations

    【讨论】:

      【解决方案2】:

      切勿直接编辑您的schema.rbschema.rb 是从数据库的当前状态自动生成的。

      您应该为此创建一个迁移文件。

      阅读Active Record Migrations

      【讨论】:

        【解决方案3】:

        您需要使用迁移而不是直接修改架构。切勿直接修改 schema.rb 文件,因为下次迁移时,它将被覆盖。 Rails 生成模式文件。在终端中尝试以下操作:

        rails generate model Book user_id:integer title:text count:integer
        

        然后运行:

        rake db:migrate
        

        这应该可以解决您的问题。您现在允许 rails 为您制作模型(这会生成迁移以制作新模型),然后迁移它会更改数据库表。看看你的schema.rb 文件后记,它应该看起来是正确的。

        有关模型生成器的更多信息,请查看here

        【讨论】:

          猜你喜欢
          • 2017-06-21
          • 1970-01-01
          • 1970-01-01
          • 2011-07-11
          • 2013-05-21
          • 2011-06-05
          • 1970-01-01
          • 2015-10-12
          • 1970-01-01
          相关资源
          最近更新 更多