相关文章推荐
豪情万千的铁板烧  ·  Copy and transform ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

PG::UndefinedTable: ERROR: relation "" does not exist - Amazon EC2, Postgresql, and Rails 5

Ask Question

There are quite a few of these question in stackoverflow none of which have been able to help me. I am launching an amazon ec2 using rails and postgresql. I have it launched but keep getting the following error (I have done everything including rake db:migrate even everything here https://stackoverflow.com/a/19804714/7039895 ):

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'
  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)
  def display_day_published
    if published_at.present?
    "Published #{published_at.strftime('%-b %-d, %Y')}"
      "Not published yet."
  def publish
    update(published: true, published_at: Time.now)
  def unpublish
    update(published: false, published_at: nil)

creatives_controller.rb (has both index and storage)

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)
    # GET /posts/1
    # GET /posts/1.json
  def show
    @campaign = storage.friendly.find(params[:id])
    @gallery = storage.friendly.find(params[:id])
  def gallery
   @galleries = storage.list_for(params[:page], params[:tag])
    @galleried = Gallery.order("created_at DESC")
    @current = Gallery.order("created_at DESC")
  private
  def storage
    Campaign.published
    Gallery.published

When I run this locally it works fine. It is when I run it in my amazon ec2 instance that it keeps giving me this error. Any help would be greatly appreciated.

Also, here is the code in the index.html.erb file:

<!-- 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>
    <%end%>
<!-- End Portfolio Box -->
                That SQL is what ActiveRecord uses to figure out the structure of the campaigns table. The error is telling you that the database that AR is connecting to doesn't have a campaigns table. Presumably you're missing a migration that would create that table or you're connecting to the wrong database.
– mu is too short
                Aug 27, 2017 at 1:36
                Are you checking that by connecting with the same credentials that Rails is, and when you do can you select from the table?
– David Aldridge
                Aug 27, 2017 at 9:06
                And when you check, how specifically are you checking? Something like select '"campaigns"'::regclass should take care of any case issues.
– mu is too short
                Aug 27, 2017 at 17:16

Figured it out. Thanks to @mu is too short I assumed I was connected to the the correct database. When I looked in the postgressql terminal:

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

It would show me this so I assumed I was connected but somehow I created a campaign database in the postgres database and not the "martin_development" database where I needed it to be created. When I connected to the "martin_development" database I found that the campaigns table was not there which caused this error. The other issue was my migrations, it turns out that I created the campaigns table in razorsql and not through rails so no migrations were created to run with rake:db migrate. I had to rails g model campaign... in rails to create the migration. Once I ran rake:db migrate the page worked again and the error disappeared.

Learned two lessons

  • Double check I am on the right database.
  • Create tables inside of rails not through an outside database ui as the migration will not be created.
  • Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.