所以我对Rails真的很新,而且我得到了一个我不理解的奇怪错误。我创建了一个Event模型,我想将它迁移到我的数据库。但是,当我运行bundle exec rake db:migrate --trace
时,这就是我得到的:
05:55 PM movienights: bundle exec rake db:migrate --trace
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Invoke rails_env (first_time)
** Execute rails_env
** Execute db:load_config
** Execute db:migrate
== CreateEvents: migrating ===================================================
-- create_table(:events)
rake aborted!
An error has occurred, all later migrations canceled:
undefined method `name' for #<ActiveRecord::ConnectionAdapters::TableDefinition:0x007ffa75a27660>
Documents/Homework/College/2011-2012/Rails/RailsApps/movienights/db/migrate/20120331213639_create_events.rb:4:in `block in change'
.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/schema_statements.rb:160:in `create_table'
.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:432:in `create_table'
.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.2/lib/active_record/migration.rb:450:in `block in method_missing'
.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.2/lib/active_record/migration.rb:424:in `block in say_with_time'
.rvm/rubies/ruby-1.9.2-p318/lib/ruby/1.9.1/benchmark.rb:295:in `measure'
.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.2/lib/active_record/migration.rb:424:in `say_with_time'
.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.2/lib/active_record/migration.rb:444:in `method_missing'
Documents/Homework/College/2011-2012/Rails/RailsApps/movienights/db/migrate/20120331213639_create_events.rb:3:in `change'
.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.2/lib/active_record/migration.rb:393:in `block (2 levels) in migrate'
.rvm/rubies/ruby-1.9.2-p318/lib/ruby/1.9.1/benchmark.rb:295:in `measure'
.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.2/lib/active_record/migration.rb:393:in `block in migrate'
.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:118:in `with_connection'
.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.2/lib/active_record/migration.rb:377:in `migrate'
.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.2/lib/active_record/migration.rb:512:in `migrate'
.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.2/lib/active_record/migration.rb:704:in `block (2 levels) in migrate'
.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.2/lib/active_record/migration.rb:761:in `call'
.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.2/lib/active_record/migration.rb:761:in `ddl_transaction'
.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.2/lib/active_record/migration.rb:703:in `block in migrate'
.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.2/lib/active_record/migration.rb:684:in `each'
.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.2/lib/active_record/migration.rb:684:in `migrate'
.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.2/lib/active_record/migration.rb:554:in `up'
.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.2/lib/active_record/migration.rb:535:in `migrate'
.rvm/gems/ruby-1.9.2-p318/gems/activerecord-3.2.2/lib/active_record/railties/databases.rake:153:in `block (2 levels) in <top (required)>'
.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `call'
.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/task.rb:205:in `block in execute'
.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `each'
.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/task.rb:200:in `execute'
.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/task.rb:158:in `block in invoke_with_call_chain'
.rvm/rubies/ruby-1.9.2-p318/lib/ruby/1.9.1/monitor.rb:201:in `mon_synchronize'
.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/task.rb:151:in `invoke_with_call_chain'
.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/task.rb:144:in `invoke'
.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/application.rb:116:in `invoke_task'
.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block (2 levels) in top_level'
.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `each'
.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/application.rb:94:in `block in top_level'
.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/application.rb:88:in `top_level'
.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/application.rb:66:in `block in run'
.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/application.rb:133:in `standard_exception_handling'
.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/lib/rake/application.rb:63:in `run'
.rvm/gems/ruby-1.9.2-p318/gems/rake-0.9.2.2/bin/rake:33:in `<top (required)>'
.rvm/gems/ruby-1.9.2-p318/bin/rake:19:in `load'
.rvm/gems/ruby-1.9.2-p318/bin/rake:19:in `<main>'
Tasks: TOP => db:migrate
当我运行bundle exec rake
时,我得到:
You have 1 pending migrations:
20120331213639 CreateEvents
Run `rake db:migrate` to update your database then try again.
我的创建事件迁移是:
class CreateEvents < ActiveRecord::Migration
def change
create_table :events do |t|
t.name :string
t.date :datetime
t.location :string
t.timestamps
end
end
end
但是,我使用rails destroy scaffold Event
删除了事件模型,并使用不同的属性重新创建了它。这可能是问题的一部分吗?
非常感谢任何帮助或建议!
答案 0 :(得分:13)
在迁移中声明属性的顺序错误。它应该是:
t.string :name
您的其他属性也一样。还要将date属性重命名为其他内容,因为它是一个您永远不应使用的保留键名。
似乎你可能像'string:name'那样运行生成器而不是正确的'name:string':)
答案 1 :(得分:1)
我有同样的错误,因为我使用大写字母而不是最小字母。字符串而不是字符串。
答案 2 :(得分:1)
我有同样的错误。我在编写可以使用的迁移时想到了#34; bool&#34;而不是布尔值。事实并非如此。当我使用rails g model model_name is_verified:bool
rails时没有抱怨设置。但当我去运行迁移时,它引发了这个错误。我刚刚在迁移文件中将其更改为t.boolean
,并且没有问题。