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内创建表,因为不会创建迁移。
相关问题