【问题标题】:PG::UndefinedTable: ERROR: relation "" does not exist - Amazon EC2, Postgresql, and Rails 5PG::UndefinedTable: 错误:关系“”不存在 - Amazon EC2、Postgresql 和 Rails 5
【发布时间】:2017-08-27 00:31:21
【问题描述】:

stackoverflow 中有很多这样的问题,没有一个能够帮助我。我正在使用 rails 和 postgresql 启动亚马逊 ec2。我已经启动了它,但一直收到以下错误(我已经完成了所有操作,包括 rake db:migrate 甚至这里的所有内容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'

以下是应用程序跟踪中的文件:

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

当我在本地运行它时,它工作正常。当我在我的亚马逊 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 -->

【问题讨论】:

  • SQL 是 ActiveRecord 用来计算campaigns 表结构的。该错误告诉您 AR 连接的数据库没有 campaigns 表。大概您错过了会创建该表的迁移,或者您连接到错误的数据库。
  • 当我检查我的数据库时,它显示它在那里。
  • 您是否通过使用与 Rails 相同的凭据进行连接来检查,何时可以从表中进行选择?
  • 当您检查时,您检查的具体程度如何?像select '"campaigns"'::regclass 这样的东西应该可以解决任何案例问题。
  • 你说得对,我没有在正确的数据库上会解释

标签: ruby-on-rails ruby postgresql amazon-web-services amazon-ec2


【解决方案1】:

想通了。感谢@mu 太短了,我以为我连接到了正确的数据库。当我查看 postgressql 终端时:

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

它会告诉我这个,所以我假设我已连接,但不知何故我在 postgres 数据库中创建了一个活动数据库,而不是我需要创建它的“martin_development”数据库。当我连接到“martin_development”数据库时,我发现活动表不存在导致此错误。另一个问题是我的迁移,结果是我在 razorsql 中创建了活动表,而不是通过 rails,因此没有创建迁移以使用rake:db migrate 运行。我必须在 rails 中 rails g model campaign... 来创建迁移。一旦我运行 rake:db migrate 页面再次工作并且错误消失了。

学到了两个教训

  1. 仔细检查我在正确的数据库中。
  2. 不要通过外部数据库 ui 在 rails 内创建表,因为不会创建迁移。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-18
    • 1970-01-01
    • 2020-05-08
    • 2021-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-06
    相关资源
    最近更新 更多