0

在stackoverflow中有相當多的這些問題,其中沒有一個能夠幫助我。我使用rails和postgresql啓動amazon ec2。我把它推出,但不斷收到以下錯誤(我所做的一切,包括耙分貝:這裏遷移,即使一切https://stackoverflow.com/a/19804714/7039895):PG :: UndefinedTable:錯誤:關係「」不存在 - Amazon EC2,Postgresql和Rails 5

ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR: relation "campaigns" does not exist 
LINE 8:    WHERE a.attrelid = '"campaigns"'::regclass 
             ^
:    SELECT a.attname, format_type(a.atttypid, a.atttypmod), 
        pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod, 
      (SELECT c.collname FROM pg_collation c, pg_type t 
       WHERE c.oid = a.attcollation AND t.oid = a.atttypid AND a.attcollation <> t.typcollation), 
        col_description(a.attrelid, a.attnum) AS comment 
       FROM pg_attribute a LEFT JOIN pg_attrdef d 
        ON a.attrelid = d.adrelid AND a.attnum = d.adnum 
       WHERE a.attrelid = '"campaigns"'::regclass 
       AND a.attnum > 0 AND NOT a.attisdropped 
       ORDER BY a.attnum 
): 

app/models/campaign.rb:15:in `block in <class:Campaign>' 
app/controllers/creatives_controller.rb:30:in `storage' 
app/controllers/creatives_controller.rb:5:in `index' 

error

下面是從應用程序跟蹤文件:

campaign.rb(模型)

class Campaign < ApplicationRecord 
    acts_as_taggable # Alias for acts_as_taggable_on :tags 

    extend FriendlyId 
    friendly_id :title, use: :slugged 

    belongs_to :author 

    has_attached_file :image, styles: { large: "600X600", medium: "300x300>", thumb: "150x150#" }, default_url: "/images/:style/missing.png" 
    validates_attachment_content_type :image, content_type: /\Aimage\/.*\z/ 

    PER_PAGE = 3 

    scope :most_recent, -> {order(published_at: :desc)} 
    scope :published, -> { where(published: true) } 
    scope :recent_paginated, -> (page) { most_recent.paginate(page: page, per_page: PER_PAGE) } 
    scope :with_tag, -> (tag) { tagged_with(tag) if tag.present? } 

    scope :list_for, -> (page, tag) do 
    recent_paginated(page).with_tag(tag) 
    end 

    def display_day_published 
    if published_at.present? 
    "Published #{published_at.strftime('%-b %-d, %Y')}" 
    else 
     "Not published yet." 
    end 
    end 

    def publish 
    update(published: true, published_at: Time.now) 
    end 

    def unpublish 
    update(published: false, published_at: nil) 

    end 
end 

creatives_controller.rb(同時具有索引和存儲)

class CreativesController < ApplicationController 
    layout "creative" 

    def index 
    @campaigns = storage.list_for(params[:page], params[:tag]) 
    @campaigned = Campaign.order("created_at DESC").limit(6) 
    @recent = Campaign.order("created_at DESC").limit(3) 

    end 

    # GET /posts/1 
    # GET /posts/1.json 
    def show 

    @campaign = storage.friendly.find(params[:id]) 
    @gallery = storage.friendly.find(params[:id]) 
    end 

    def gallery 
    @galleries = storage.list_for(params[:page], params[:tag]) 
    @galleried = Gallery.order("created_at DESC") 
    @current = Gallery.order("created_at DESC") 

    end 


    private 

    def storage 
    Campaign.published 
    Gallery.published 

    end 

end 

當我在本地運行它時,它工作正常。當我在我的amazon ec2實例中運行它時,它一直給我這個錯誤。任何幫助將不勝感激。

而且,這裏是在index.html.erb文件中的代碼:

<!-- Portfolio Box --> 
<ul class="list-unstyled row portfolio-box"> 
<% @recent.each do |campaign| %> 
    <li class="col-sm-4"> 
     <%= link_to campaign.image.url, :class => "thumbnail fancybox", data: { rel: "gallery" }, title: campaign.title do %> 
     <%= image_tag campaign.image, :class => "full-width img-responsive" %> 
     <span class="rounded-x portfolio-box-in"> 
      <i class="fa fa-search-plus"></i> 
     </span> 
     <% end %> 
     <div class="headline-left margin-bottom-10"><h3 class="headline-brd"><%= campaign.title %></h3></div> 
     <small class="project-tag"><i class="fa fa-tags"></i><%= raw campaign.tags.map{ |t| link_to t.name, campaigns_path(tag: t.name)}.join(', ') %></small> 
     <p><%= truncate(campaign.description, length: 130) %> </p> 
    </li> 
    <%end%> 
</ul> 
<!-- End Portfolio Box --> 
+0

SQL是ActiveRecord用來確定「campaign」表的結構的。該錯誤告訴你AR所連接的數據庫沒有「campaign」表。大概你錯過了一個會創建該表的遷移,或者你連接到了錯誤的數據庫。 –

+0

當我檢查我的數據庫時,它顯示它在那裏。 –

+0

您是否通過連接Rails所用的相同憑據來檢查該問題,並且您可以從表中選擇? –

回答

0

想通了。由於@mu太短,我認爲我連接到了正確的數據庫。當我在PostgreSQL的終端看:

postgres=# \dt; 
      List of relations 
Schema | Name | Type | Owner 
--------+-----------+-------+---------- 
public | campaigns | table | postgres 
(1 row) 

它會告訴我這個,所以我認爲我已連接,但不知何故,我創建Postgres數據庫競選數據庫,而不是地方,我需要它是「martin_development」數據庫創建。當我連接到「martin_development」數據庫時,我發現廣告系列表不存在,導致此錯誤。另一個問題是我的遷移,事實證明,我在razorsql中創建了廣告系列表,而不是通過導軌,因此沒有創建遷移用rake:db migrate運行。我不得不rails g model campaign ...在軌道上創建遷移。一旦我運行rake:db遷移頁面再次工作,錯誤消失。

教訓兩個教訓

  1. 仔細檢查,我在正確的數據庫。
  2. 不通過外部數據庫ui在rails內創建表,因爲不會創建遷移。
相關問題