将Postgresql添加到现有的Rails项目

时间:2019-03-28 03:06:38

标签: ruby-on-rails postgresql

我正在遵循本指南https://devcenter.heroku.com/articles/getting-started-with-rails5。我取出“ sqlite3” gem并添加“ pg”,然后运行捆绑安装。然后,将config / database.yml文件更改为以下

config / database.yml

default: &default
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>

development:
  <<: *default
  database: myapp5_development

test:
  <<: *default
  database: myapp5_test

production:
  <<: *default
  database: myapp5_production
  username: myapp5
  password: <%= ENV['MYAPP_DATABASE_PASSWORD'] %>

对于下一部分,该指南为我提供了两种选择,并且我都尝试过。 安装此gem'rails_12factor'或将以下代码添加到我的'config / environments / production.rb'文件中

config / environments / production.rb

config.public_file_server.enabled =     ENV['RAILS_SERVE_STATIC_FILES'].present?

if ENV["RAILS_LOG_TO_STDOUT"].present?
  logger           = ActiveSupport::Logger.new(STDOUT)
  logger.formatter = config.log_formatter
  config.logger = ActiveSupport::TaggedLogging.new(logger)
end

然后,如果我先运行rake db:create,然后再运行rake db:migrate,它会给我错误,因此我运行rake db:reset,这样就可以进行迁移。如果启动我的网站,我会收到错误

PG::UndefinedTable: ERROR:  relation "videos" does not exist
LINE 1: SELECT  "videos".* FROM "videos" ORDER BY "videos"."title" A...
                            ^
: SELECT  "videos".* FROM "videos" ORDER BY "videos"."title" ASC LIMIT $1 OFFSET $2
Extracted source (around line #2):

<div class="container">
<% @videos.each do |x| %>
<p> <div class="child">  
  <video controls width="310" height="230" src="<%= x.file %>"></video>
    <p> <%= x.title %> </p>

当我通过ActiveRecord :: Base.connection.tables查看表时,我发现视频存在。

[“视频”,“ ipaddresstrackers”,“用户”,“投票”,“ schema_migrations”,“ ar_internal_metadata”]

1 个答案:

答案 0 :(得分:1)

将“视频”表重命名为“视频”。

  

在PostgreSQL中,未加引号的名称不区分大小写。因此SELECT * FROM videosSELECT * FROM ViDeOs是等效的。

     

但是,带引号的名称区分大小写。 SELECT * FROM "videos"不等同于SELECT * FROM "Videos"

更多详细信息here