简体   繁体   中英

rake db:migrate syntax error when trying to do migration

I get this error when I try to do 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)

Here is my migrate table:

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

Anyone knows why this is happening and how I can fix it so that when I do migration it does not give me this error?

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

Remove all this from the start of your migration file: bundle exec rake test:prepare

bundle exec rake test:prepare is run from the command line. Broken down, bundle exec executes a script in the context of the current bundle; rake test:prepare (or more likely, rake db:test:prepare ) is the rake task to be executed.

Your migration should read as follows:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM