【问题标题】:Ruby on Rails - error running serverRuby on Rails - 运行服务器时出错
【发布时间】:2012-07-31 06:37:18
【问题描述】:

我目前正在做一个 Rails 项目。当我尝试启动 rails 服务器时,它会抛出以下错误:

=> Booting WEBrick
=> Rails 3.1.3 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Exiting
/var/lib/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/connection_adapters      
/sqlite_adapter.rb:439:in `table_structure': Could not find table 'dbrick'   
(ActiveRecord::StatementInvalid)

我的表名是'dbrick'。我还尝试 rake db:drop 和 rake db:mirgrate。迁移时抛出以下错误:

rake aborted!
Could not find table 'dbrick'

Tasks: TOP => db:migrate => environment
(See full trace by running task with --trace)

这是我的迁移文件:

class CreateDbricks < ActiveRecord::Migration
def self.up
  create_table :dbricks do |t|
    t.text :description
    t.string :video
    t.string :video_html
    t.string :image_id
    t.string :option_id
    t.boolean :choice
    t.string :reach
    t.integer :category_id
    t.string :user_id
    t.datetime :deleted_at

    t.timestamps
  end
end

 def self.down
   drop_table :dbricks
 end
end

如果有人能帮我解决这个问题,那将是非常有帮助的。 提前致谢。

【问题讨论】:

  • 你有一个错字:迁移创建dbricks,但代码尝试使用dbrick 表。 (复数与单数形式)
  • 感谢您的回复。是的,我尝试过 rake db:schema:load,但它给出了相同的错误,即 rake 中止!找不到表 'dbrick' 任务:TOP => db:migrate => 环境(通过使用 --trace 运行任务查看完整跟踪)
  • 我应该在哪里更改代码以使 dbrick 变为复数形式,提前致谢

标签: ruby-on-rails rake table-structure


【解决方案1】:

我会尝试:

rake db:schema:load

加载您的架构(我相信它会针对您的数据库找到错误)。

如果失败,我将手动找到创建您的 dbrick 的迁移,找到文件名并复制并粘贴文件名中的数字以生成此:

rake db:migrate:down VERSION=123412341234 # <-- where the number is the number you pasted

查找错误。有时一件事已经存在,或者不存在并且阻止迁移一直运行,因此这将是您错误的根源。如果成功,则将其重新耙起来:

rake db:migrate:up VERSION=123412341234 # <-- where the number is the number you pasted

如果没有成功,那么你必须戴上你的矿工头盔,然后把手弄脏:

rails dbconsole

这将带您进入数据库,您必须手动删除阻止迁移发生的任何表/列。一旦修复,退出并rake db:migrate:up

【讨论】:

  • 非常感谢..明白了..你的回答对我帮助很大..谢谢
  • 太棒了!解决方案是什么?如果值得,不要忘记放弃投票并将答案设置为正确。
  • 我刚刚做了 rake db:migrate down 和 db:migrate up.. 非常感谢它的工作
  • 没有赞或标记答案? tsk tsk tsk .. :) 很高兴它成功了!
【解决方案2】:

您是否已迁移数据库? rake db:migrate

如果有,请删除数据库(这会删除所有数据,所以要小心 - 如果您不关心丢失数据库中的数据,请执行此操作)

rake db:drop

这将清除您的数据库和架构。那么

rake db:migrate

这将重新迁移您的架构。

【讨论】:

  • 感谢您的回复.. 是的,我按照您的说法删除并迁移了.. 删除已完成,但在迁移时出现错误 rake aborted! Could not find table 'dbrick' Tasks: TOP =&gt; db:migrate =&gt; environment (See full trace by running task with --trace)
  • @Abhiram 这很奇怪。也许您的迁移之一是在创建 dbrick create 迁移之前对表 dbrick 执行某些操作。
猜你喜欢
  • 1970-01-01
  • 2015-10-21
  • 1970-01-01
  • 1970-01-01
  • 2018-07-07
  • 2011-10-10
  • 1970-01-01
  • 2014-10-31
相关资源
最近更新 更多