新手问题 Rails ActiveRecord::PendingMigrationError 错误

736248591 · 2015年09月06日 · 最后由 davidleeqk 回复于 2016年07月09日 · 4142 次阅读

当我输入 rails s 访问 http://localhost:3000/ 时 出错误

ActiveRecord::PendingMigrationError 
Migrations are pending. To resolve this issue, run: bin/rake db:migrate RAILS_ENV=development 
Rails.root: C:/Users/zmk pc/blog
Application Trace | Framework Trace | Full Trace 
activerecord (4.2.3) lib/active_record/migration.rb:392:in `check_pending!'
activerecord (4.2.3) lib/active_record/migration.rb:373:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.2.3) lib/active_support/callbacks.rb:84:in `run_callbacks'
actionpack (4.2.3) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
web-console (2.2.1) lib/web_console/middleware.rb:39:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.3) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.3) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.3) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.3) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.3) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.3) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.4) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.3) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/static.rb:116:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.2.3) lib/rails/engine.rb:518:in `call'
railties (4.2.3) lib/rails/application.rb:165:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
rack (1.6.4) lib/rack/content_length.rb:15:in `call'
rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'
D:/Ruby21-x64/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
D:/Ruby21-x64/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
D:/Ruby21-x64/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'

按照提示,我停止 rails 输入 rake db:migrate RAILS_ENV=development 弹出错误

== 20150906003155 CreateArticles: migrating ===================================
-- create_table(:articles)
rake aborted!
StandardError: An error has occurred, all later migrations canceled:

Mysql2::Error: Table 'articles' already exists: CREATE TABLE `articles` (`id` in
t(11) auto_increment PRIMARY KEY, `content` text, `created_at` datetime NOT NULL
, `updated_at` datetime NOT NULL) ENGINE=InnoDBC:/Users/zmk pc/blog/db/migrate/2
0150906003155_create_articles.rb:3:in `change'
D:in `migrate'
ActiveRecord::StatementInvalid: Mysql2::Error: Table 'articles' already exists:
CREATE TABLE `articles` (`id` int(11) auto_increment PRIMARY KEY, `content` text
, `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL) ENGINE=InnoDB
C:/Users/zmk pc/blog/db/migrate/20150906003155_create_articles.rb:3:in `change'
D:in `migrate'
Mysql2::Error: Table 'articles' already exists
C:/Users/zmk pc/blog/db/migrate/20150906003155_create_articles.rb:3:in `change'
D:in `migrate'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

\blog\db\migrate\20150906003155_create_articles.rb 的代码如下

class CreateArticles < ActiveRecord::Migration
  def change
    create_table :articles do |t|
      t.varchar :name
      t.varchar :title
      t.text :content

      t.timestamps null: false
    end
  end
end

以下是我整个 blog 文件夹的下载地址 http://pan.baidu.com/s/1bnqbXmR

求高手指点~!

@Kabie 大神,求助!

rake db:reset 注意这会清空你项目数据库然后重新建立

#2 楼 @kikyous 我输入 rake db:reset 抛出错误

Database 'testblog' does not exist
testblog already exists
C:/Users/zmk pc/blog/db/schema.rb doesn't exist yet. Run `rake db:migrate` to c
eate it, then try again. If you do not intend to use a database, you should ins
ead alter C:/Users/zmk pc/blog/config/application.rb to limit the frameworks th
t will be loaded.

按照提示我输入 rake db:migrate

== 20150906003155 CreateArticles: migrating ===================================
-- create_table(:articles)
   -> 0.0052s
== 20150906003155 CreateArticles: migrated (0.0052s) ==========================

然后重试 rake db:reset

Database 'testblog' does not exist
testblog already exists
-- create_table("articles", {:force=>:cascade})
   -> 1.0321s
-- initialize_schema_migrations_table()
   -> 0.0360s
-- create_table("articles", {:force=>:cascade})
   -> 1.0441s
-- initialize_schema_migrations_table()
   -> 0.0040s

之后我用 rails s 然而还是抛出错误

ExecJS::ProgramError in Home#index
Showing C:/Users/zmk pc/blog/app/views/layouts/application.html.erb where line #6 raised:

TypeError: 对象不支持此属性或方法
Rails.root: C:/Users/zmk pc/blog

Application Trace | Framework Trace | Full Trace
execjs (2.5.2) lib/execjs/external_runtime.rb:39:in `exec'
execjs (2.5.2) lib/execjs/external_runtime.rb:21:in `eval'
execjs (2.5.2) lib/execjs/external_runtime.rb:46:in `call'
coffee-script (2.4.1) lib/coffee_script.rb:78:in `compile'
sprockets (3.2.0) lib/sprockets/coffee_script_processor.rb:21:in `block in call'
sprockets (3.2.0) lib/sprockets/cache.rb:85:in `fetch'
sprockets (3.2.0) lib/sprockets/coffee_script_processor.rb:20:in `call'
sprockets (3.2.0) lib/sprockets/processor_utils.rb:75:in `call_processor'
sprockets (3.2.0) lib/sprockets/processor_utils.rb:57:in `block in call_processors'
sprockets (3.2.0) lib/sprockets/processor_utils.rb:56:in `reverse_each'
sprockets (3.2.0) lib/sprockets/processor_utils.rb:56:in `call_processors'
sprockets (3.2.0) lib/sprockets/loader.rb:86:in `load_asset_by_uri'
sprockets (3.2.0) lib/sprockets/loader.rb:45:in `block in load'
sprockets (3.2.0) lib/sprockets/loader.rb:155:in `fetch_asset_from_dependency_cache'
sprockets (3.2.0) lib/sprockets/loader.rb:38:in `load'
sprockets (3.2.0) lib/sprockets/cached_environment.rb:20:in `block in initialize'
sprockets (3.2.0) lib/sprockets/cached_environment.rb:47:in `yield'
sprockets (3.2.0) lib/sprockets/cached_environment.rb:47:in `load'
sprockets (3.2.0) lib/sprockets/bundle.rb:23:in `block in call'
sprockets (3.2.0) lib/sprockets/utils.rb:183:in `dfs'
sprockets (3.2.0) lib/sprockets/bundle.rb:24:in `call'
sprockets (3.2.0) lib/sprockets/processor_utils.rb:75:in `call_processor'
sprockets (3.2.0) lib/sprockets/processor_utils.rb:57:in `block in call_processors'
sprockets (3.2.0) lib/sprockets/processor_utils.rb:56:in `reverse_each'
sprockets (3.2.0) lib/sprockets/processor_utils.rb:56:in `call_processors'
sprockets (3.2.0) lib/sprockets/loader.rb:86:in `load_asset_by_uri'
sprockets (3.2.0) lib/sprockets/loader.rb:45:in `block in load'
sprockets (3.2.0) lib/sprockets/loader.rb:155:in `fetch_asset_from_dependency_cache'
sprockets (3.2.0) lib/sprockets/loader.rb:38:in `load'
sprockets (3.2.0) lib/sprockets/cached_environment.rb:20:in `block in initialize'
sprockets (3.2.0) lib/sprockets/cached_environment.rb:47:in `yield'
sprockets (3.2.0) lib/sprockets/cached_environment.rb:47:in `load'
sprockets (3.2.0) lib/sprockets/base.rb:63:in `find_asset'
sprockets (3.2.0) lib/sprockets/environment.rb:30:in `find_asset'
sprockets (3.2.0) lib/sprockets/base.rb:89:in `[]'
sprockets-rails (2.3.2) lib/sprockets/rails/helper.rb:230:in `lookup_asset_for_path'
sprockets-rails (2.3.2) lib/sprockets/rails/helper.rb:190:in `check_errors_for'
sprockets-rails (2.3.2) lib/sprockets/rails/helper.rb:137:in `block in javascript_include_tag'
sprockets-rails (2.3.2) lib/sprockets/rails/helper.rb:136:in `map'
sprockets-rails (2.3.2) lib/sprockets/rails/helper.rb:136:in `javascript_include_tag'
app/views/layouts/application.html.erb:6:in `_app_views_layouts_application_html_erb__1871698029_64944040'
actionview (4.2.3) lib/action_view/template.rb:145:in `block in render'
activesupport (4.2.3) lib/active_support/notifications.rb:166:in `instrument'
actionview (4.2.3) lib/action_view/template.rb:333:in `instrument'
actionview (4.2.3) lib/action_view/template.rb:143:in `render'
actionview (4.2.3) lib/action_view/renderer/template_renderer.rb:66:in `render_with_layout'
actionview (4.2.3) lib/action_view/renderer/template_renderer.rb:52:in `render_template'
actionview (4.2.3) lib/action_view/renderer/template_renderer.rb:14:in `render'
actionview (4.2.3) lib/action_view/renderer/renderer.rb:42:in `render_template'
actionview (4.2.3) lib/action_view/renderer/renderer.rb:23:in `render'
actionview (4.2.3) lib/action_view/rendering.rb:100:in `_render_template'
actionpack (4.2.3) lib/action_controller/metal/streaming.rb:217:in `_render_template'
actionview (4.2.3) lib/action_view/rendering.rb:83:in `render_to_body'
actionpack (4.2.3) lib/action_controller/metal/rendering.rb:32:in `render_to_body'
actionpack (4.2.3) lib/action_controller/metal/renderers.rb:37:in `render_to_body'
actionpack (4.2.3) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (4.2.3) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (4.2.3) lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render'
activesupport (4.2.3) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
D:/Ruby21-x64/lib/ruby/2.1.0/benchmark.rb:294:in `realtime'
activesupport (4.2.3) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (4.2.3) lib/action_controller/metal/instrumentation.rb:44:in `block in render'
actionpack (4.2.3) lib/action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime'
activerecord (4.2.3) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
actionpack (4.2.3) lib/action_controller/metal/instrumentation.rb:43:in `render'
actionpack (4.2.3) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (4.2.3) lib/action_controller/metal/implicit_render.rb:5:in `send_action'
actionpack (4.2.3) lib/abstract_controller/base.rb:198:in `process_action'
actionpack (4.2.3) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.2.3) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.2.3) lib/active_support/callbacks.rb:115:in `call'
activesupport (4.2.3) lib/active_support/callbacks.rb:115:in `call'
activesupport (4.2.3) lib/active_support/callbacks.rb:553:in `block (2 levels) in compile'
activesupport (4.2.3) lib/active_support/callbacks.rb:503:in `call'
activesupport (4.2.3) lib/active_support/callbacks.rb:503:in `call'
activesupport (4.2.3) lib/active_support/callbacks.rb:88:in `run_callbacks'
actionpack (4.2.3) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.2.3) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.2.3) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (4.2.3) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.3) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.3) lib/active_support/notifications.rb:164:in `instrument'
actionpack (4.2.3) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.2.3) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
activerecord (4.2.3) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.2.3) lib/abstract_controller/base.rb:137:in `process'
actionview (4.2.3) lib/action_view/rendering.rb:30:in `process'
actionpack (4.2.3) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.2.3) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.2.3) lib/action_controller/metal.rb:237:in `block in action'
actionpack (4.2.3) lib/action_dispatch/routing/route_set.rb:76:in `call'
actionpack (4.2.3) lib/action_dispatch/routing/route_set.rb:76:in `dispatch'
actionpack (4.2.3) lib/action_dispatch/routing/route_set.rb:45:in `serve'
actionpack (4.2.3) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.3) lib/action_dispatch/journey/router.rb:30:in `each'
actionpack (4.2.3) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.3) lib/action_dispatch/routing/route_set.rb:821:in `call'
rack (1.6.4) lib/rack/etag.rb:24:in `call'
rack (1.6.4) lib/rack/conditionalget.rb:25:in `call'
rack (1.6.4) lib/rack/head.rb:13:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/flash.rb:260:in `call'
rack (1.6.4) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.6.4) lib/rack/session/abstract/id.rb:220:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/cookies.rb:560:in `call'
activerecord (4.2.3) lib/active_record/query_cache.rb:36:in `call'
activerecord (4.2.3) lib/active_record/connection_adapters/abstract/connection_pool.rb:653:in `call'
activerecord (4.2.3) lib/active_record/migration.rb:377:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.2.3) lib/active_support/callbacks.rb:84:in `run_callbacks'
actionpack (4.2.3) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/reloader.rb:73:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
web-console (2.2.1) lib/web_console/middleware.rb:39:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
railties (4.2.3) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.3) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.3) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.3) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.3) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.3) lib/rails/rack/logger.rb:20:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/request_id.rb:21:in `call'
rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
rack (1.6.4) lib/rack/runtime.rb:18:in `call'
activesupport (4.2.3) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
actionpack (4.2.3) lib/action_dispatch/middleware/static.rb:116:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
railties (4.2.3) lib/rails/engine.rb:518:in `call'
railties (4.2.3) lib/rails/application.rb:165:in `call'
rack (1.6.4) lib/rack/lock.rb:17:in `call'
rack (1.6.4) lib/rack/content_length.rb:15:in `call'
rack (1.6.4) lib/rack/handler/webrick.rb:88:in `service'
D:/Ruby21-x64/lib/ruby/2.1.0/webrick/httpserver.rb:138:in `service'
D:/Ruby21-x64/lib/ruby/2.1.0/webrick/httpserver.rb:94:in `run'
D:/Ruby21-x64/lib/ruby/2.1.0/webrick/server.rb:295:in `block in start_thread'

\blog\app\views\layouts\application.html.erb 代码如下

<!DOCTYPE html>
<html>
<head>
  <title>Blog</title>
  <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
  <%= csrf_meta_tags %>
</head>
<body>

<%= yield %>

</body>
</html>

#3 楼 @736248591 不推荐在 win 下做 rails 开发,感觉走不远的,会遇到各种问题,建议 linux 或 mac。 你现在的这个问题就是在 win 下才会遇到的。

把 Gemfile.lock 里的 coffee-script-source (1.9.1) 换成 coffee-script-source (1.8.0),然后重新 bundle install

参考 https://ruby-china.org/topics/24343

#4 楼 @kikyous 我发现我删掉这行代码就可以了

<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>

发现,之前我也删掉一行有关 JavaScript 的代码 好像用到 JavaScript 就会抛出错误,这个是为什么?

#5 楼 @736248591 coffeescript 某个版本在 win 下有问题

#6 楼 @kikyous 诶,这样啊。 非常感谢指点!

drop 再 create 吧。。。。

这个问题我一开始在 windows 上也遇到过,真的不建议在 win 上玩 rails

我是用 virtual box 虚拟个 ubuntu server 玩的,你也可以用 docker 玩,幸亏一开始不去踩 win 的坑

别用 64 位版,装 node.js 运行时,Windows 下其实还行。

你可以在 Windows 下开发。但是最好别在 Windows 下运行。

首先,rake db:reset 并不是重新建数据库,文档上的解释:

The rake db:setup task will create the database, load the schema and initialize it with the seed data.

如果在开发的时候随手改写了 migration,reset 并不会使用最新的 schema,这时候需要重新的 db:drop db:create db:migrate。

在 win 上开发,建议用 Virtual box + vagrant,避免折腾开发环境,我公司开发组其他五个人都是用 win 开发 python,配合 vagrant 调试 rails。

#13 楼 @liwei78 哥们,你眼花了😄

这里是两个问题吧 第一次是 migration 第二次是 资源引入

#13 楼 @liwei78 我现在也是这个问题,请问具体怎么操作才能解决这个问题!

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册