【问题标题】:Strange ActiveRecord::StatementInvalid error for Rails 3.2 on UbuntuUbuntu 上 Rails 3.2 的奇怪 ActiveRecord::StatementInvalid 错误
【发布时间】:2013-03-30 05:52:22
【问题描述】:

我是 Rails 新手。我正在开发一个新的 Rails 项目,它是一个图书馆系统,同时尝试访问 Loan_fines 我收到一个奇怪的错误。

错误如下:-

ActiveRecord::StatementInvalid in LoanFinesController#index

PG::Error: ERROR:  relation "loan_fines" does not exist
LINE 5:              WHERE a.attrelid = '"loan_fines"'::regclass
                                        ^
:             SELECT a.attname, format_type(a.atttypid, a.atttypmod),
                     pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
              FROM pg_attribute a LEFT JOIN pg_attrdef d
                ON a.attrelid = d.adrelid AND a.attnum = d.adnum
             WHERE a.attrelid = '"loan_fines"'::regclass
               AND a.attnum > 0 AND NOT a.attisdropped
             ORDER BY a.attnum

我的 LoanFines 控制器如下:-

class LoanFinesController < ApplicationController
  # GET /loan_fines
  # GET /loan_fines.json
  def index
    @loan_fines = LoanFine.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @loan_fines }
    end
  end

  # GET /loan_fines/1
  # GET /loan_fines/1.json
  def show
    @loan_fine = LoanFine.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @loan_fine }
    end
  end

  # GET /loan_fines/new
  # GET /loan_fines/new.json
  def new
    @loan_fine = LoanFine.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @loan_fine }
    end
  end

  # GET /loan_fines/1/edit
  def edit
    @loan_fine = LoanFine.find(params[:id])
  end

  # POST /loan_fines
  # POST /loan_fines.json
  def create
    @loan_fine = LoanFine.new(params[:loan_fine])

    respond_to do |format|
      if @loan_fine.save
        format.html { redirect_to @loan_fine, notice: 'Loan fine was successfully created.' }
        format.json { render json: @loan_fine, status: :created, location: @loan_fine }
      else
        format.html { render action: "new" }
        format.json { render json: @loan_fine.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /loan_fines/1
  # PUT /loan_fines/1.json
  def update
    @loan_fine = LoanFine.find(params[:id])

    respond_to do |format|
      if @loan_fine.update_attributes(params[:loan_fine])
        format.html { redirect_to @loan_fine, notice: 'Loan fine was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @loan_fine.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /loan_fines/1
  # DELETE /loan_fines/1.json
 def delete
    @loan_fine = LoanFine.find(params[:id])
    @loan_fine.deleted = 1
    @loan_fine.save

    respond_to do |format|
      format.html { redirect_to loan_fines_url }
      format.json { render :json => { :success => true } }
    end
  end 
end

我的 Loan Fines id 索引如下:-

<h1 class="List">Listing Loan Fines</h1>

<table class="table table-bordered" >
  <tr>
    <th>ID</th>
    <th>Category Id</th>
    <th>Loan Duration</th>
    <th>Book Id</th>
    <th>Fine Amount</th>
    <th>Fine Duration</th>
    <th>Edit</th>
    <th>Delete</th>
  </tr>

<% @loan_fines.each do |c| %>
  <tr>
    <td><%= link_to c.id, {:action => 'show', :id => c.id} %> &nbsp;</td>
    <td><%= c.category_id %>&nbsp;</td>
    <td><%= c.loan_duration %>&nbsp;</td>
    <td><%= c.book_id %>&nbsp;</td>
    <td><%= c.fine_amount %>&nbsp;</td>
    <td><%= c.fine_duration %>&nbsp;</td>
    <td><%= link_to 'Edit', {:action => 'edit', :id => c.id} %> &nbsp;</td>
    <td><%= link_to 'Delete', {:action => 'delete', :id => c.id},
    :data => { :confirm => "Are you sure you want to delete this value?" } %></td>
    </tr>
<% end %>
</table>
<br />

<%= link_to 'New Loan fine', new_loan_fine_path %>

我的日志有以下内容:-

Started GET "/loan_fines" for 127.0.0.1 at 2013-03-29 22:36:37 -0700
Processing by LoanFinesController#index as HTML
PG::Error: ERROR:  relation "loan_fines" does not exist
LINE 5:              WHERE a.attrelid = '"loan_fines"'::regclass
                                        ^
:             SELECT a.attname, format_type(a.atttypid, a.atttypmod),
                     pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
              FROM pg_attribute a LEFT JOIN pg_attrdef d
                ON a.attrelid = d.adrelid AND a.attnum = d.adnum
             WHERE a.attrelid = '"loan_fines"'::regclass
               AND a.attnum > 0 AND NOT a.attisdropped
             ORDER BY a.attnum

Completed 500 Internal Server Error in 1ms

ActiveRecord::StatementInvalid (PG::Error: ERROR:  relation "loan_fines" does not exist
LINE 5:              WHERE a.attrelid = '"loan_fines"'::regclass
                                        ^
:             SELECT a.attname, format_type(a.atttypid, a.atttypmod),
                     pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
              FROM pg_attribute a LEFT JOIN pg_attrdef d
                ON a.attrelid = d.adrelid AND a.attnum = d.adnum
             WHERE a.attrelid = '"loan_fines"'::regclass
               AND a.attnum > 0 AND NOT a.attisdropped
             ORDER BY a.attnum
):
  app/controllers/loan_fines_controller.rb:5:in `index'


  Rendered /home/chiron/.rvm/gems/ruby-1.9.3-p327/gems/actionpack-3.2.11/lib/action_dispatch/middleware/templates/rescues/_trace.erb (5.2ms)
  Rendered /home/chiron/.rvm/gems/ruby-1.9.3-p327/gems/actionpack-3.2.11/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.0ms)
  Rendered /home/chiron/.rvm/gems/ruby-1.9.3-p327/gems/actionpack-3.2.11/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (11.3ms)

我陷入了困境。最奇怪的部分是work on windows 似乎是相同的代码,但在尝试Ubuntu am getting an error 时。

谁能帮帮我。

【问题讨论】:

  • 你也应该添加你的模型......
  • 您的 PostgreSQL 数据库中有一个 LoanFine 模型,但没有 load_fines 表。
  • @muistooshort 我在数据库中有表
  • 如果这样做,您将不会收到 relation "loan_fines" does not exist 错误。使用psql 访问您的数据库,比如select '"load_fines"'::regclassselect * from load_fines,您会得到同样的错误。
  • 一切顺利@Catmandu

标签: ruby-on-rails ruby ruby-on-rails-3 ubuntu ruby-on-rails-3.2


【解决方案1】:

看起来像 db 错误。首先运行rake db:migraterake test:prepare

【讨论】:

  • 我做了 rake db:migrate。它显示 Db 已经存在。我现在该怎么办
  • 一定是db错误。如果 rake taks 无法提供帮助,您需要手动检查您的 db 设置和 db 连接以查看它们是否正确。
猜你喜欢
  • 2013-06-07
  • 1970-01-01
  • 2013-05-20
  • 1970-01-01
  • 2013-10-31
  • 2013-09-06
  • 1970-01-01
  • 1970-01-01
  • 2014-04-28
相关资源
最近更新 更多