【问题标题】:rake db:migrate syntax error when trying to do migrationrake db:migrate 尝试迁移时出现语法错误
【发布时间】:2013-11-28 06:52:38
【问题描述】:

当我尝试执行 rake db:migrate: 时出现此错误:

rake aborted!
/Users/farhadam/mboddy/db/migrate/20131113032404_create_microposts.rb:1: syntax error, unexpected tCONSTANT, expecting keyword_do or '{' or '('
...t:prepareclass CreateMicroposts < ActiveRecord::Migration
...                               ^
/Users/farhadam/mboddy/db/migrate/20131113032404_create_microposts.rb:10: syntax error, unexpected keyword_end, expecting $end
/Users/farhadam/.rvm/gems/ruby-1.9.3-p327/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `require'
/Users/farhadam/.rvm/gems/ruby-1.9.3-p327/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `block in require'
/Users/farhadam/.rvm/gems/ruby-1.9.3-p327/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:213:in `load_dependency'
/Users/farhadam/.rvm/gems/ruby-1.9.3-p327/gems/activesupport-4.0.0/lib/active_support/dependencies.rb:228:in `require'
/Users/farhadam/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-4.0.0/lib/active_record/migration.rb:718:in `load_migration'
/Users/farhadam/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-4.0.0/lib/active_record/migration.rb:714:in `migration'
/Users/farhadam/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-4.0.0/lib/active_record/migration.rb:708:in `disable_ddl_transaction'
/Users/farhadam/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-4.0.0/lib/active_record/migration.rb:1012:in `use_transaction?'
/Users/farhadam/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-4.0.0/lib/active_record/migration.rb:1004:in `ddl_transaction'
/Users/farhadam/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-4.0.0/lib/active_record/migration.rb:958:in `execute_migration_in_transaction'
/Users/farhadam/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-4.0.0/lib/active_record/migration.rb:920:in `block in migrate'
/Users/farhadam/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-4.0.0/lib/active_record/migration.rb:916:in `each'
/Users/farhadam/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-4.0.0/lib/active_record/migration.rb:916:in `migrate'
/Users/farhadam/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-4.0.0/lib/active_record/migration.rb:764:in `up'
/Users/farhadam/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-4.0.0/lib/active_record/migration.rb:742:in `migrate'
/Users/farhadam/.rvm/gems/ruby-1.9.3-p327/gems/activerecord-4.0.0/lib/active_record/railties/databases.rake:42:in `block (2 levels) in <top (required)>'
/Users/farhadam/.rvm/gems/ruby-1.9.3-p327/bin/ruby_noexec_wrapper:14:in `eval'
/Users/farhadam/.rvm/gems/ruby-1.9.3-p327/bin/ruby_noexec_wrapper:14:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

这是我的迁移表:

bundle exec rake test:prepare
class CreateMicroposts < ActiveRecord::Migration
  def change
    create_table :microposts do |t|
      t.string :content
      t.integer :user_id
      t.timestamps
    end
    add_index :microposts,[:user_id,:created_at]
  end
end

任何人都知道为什么会发生这种情况以及如何解决它,以便在我进行迁移时它不会给我这个错误?

【问题讨论】:

  • 这有什么好运气吗?

标签: ruby-on-rails rake syntax-error database-migration


【解决方案1】:

bundle exec rake test:prepare 不是 Ruby - 它不应该在您的迁移文件中。

【讨论】:

    【解决方案2】:

    从迁移文件的开头删除所有这些:bundle exec rake test:prepare

    bundle exec rake test:prepare 从命令行运行。分解,bundle exec 在当前包的上下文中执行一个脚本; rake test:prepare(或更可能是rake db:test:prepare)是要执行的 rake 任务。

    您的迁移应如下所示:

    class CreateMicroposts < ActiveRecord::Migration
        def change
            create_table :microposts do |t|
                t.string :content
                t.integer :user_id
                t.timestamps
            end
            add_index :microposts,[:user_id,:created_at]
        end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-19
      • 2017-02-05
      • 1970-01-01
      • 2021-07-29
      • 1970-01-01
      • 1970-01-01
      • 2015-07-10
      • 2017-10-04
      相关资源
      最近更新 更多