使用小型 Ruby 脚本来创建数据并将数据存储到 Postgres 数据库。在创建和删除数据库或运行迁移时,该应用程序运行良好。但是,当我尝试运行 rake db:seed
时它失败了。错误消息表明该表不存在...但与此同时,如果我运行错误消息中引用的相同 SQL 脚本,但在 pgAdmin 中,它会返回有效结果。我想知道授予 database.yml
中命名的用户的权限是否有问题,如果有,我将如何在脚本中解决这个问题?任何有助于理解这一点的帮助将不胜感激。
这是我在终端中收到的错误消息:
acmecorp$ rake db:seed
rake aborted!
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "applications" does not exist
LINE 8: WHERE a.attrelid = '"applications"'::regclass
^
: SELECT a.attname, format_type(a.atttypid, a.atttypmod),
pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod,
c.collname, 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
LEFT JOIN pg_type t ON a.atttypid = t.oid
LEFT JOIN pg_collation c ON a.attcollation = c.oid AND a.attcollation <> t.typcollation
WHERE a.attrelid = '"applications"'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
/Users/thisguy/repositories/acmecorp/db/seeds.rb:1:in `<top (required)>'
/Users/thisguy/repositories/acmecorp/rakefile.rb:30:in `require_relative'
/Users/thisguy/repositories/acmecorp/rakefile.rb:30:in `block (2 levels) in <top (required)>'
Caused by:
PG::UndefinedTable: ERROR: relation "applications" does not exist
LINE 8: WHERE a.attrelid = '"applications"'::regclass
^
/Users/thisguy/repositories/acmecorp/db/seeds.rb:1:in `<top (required)>'
/Users/thisguy/repositories/acmecorp/rakefile.rb:30:in `require_relative'
/Users/thisguy/repositories/acmecorp/rakefile.rb:30:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)
我的database.yml文件:
# config/database.yml
default: &default
host: 'localhost'
adapter: 'postgresql'
encoding: utf-8
development:
<<: *default
database: 'acmecorp_dev'
username: 'acmecorpapp'
Gemfile 非常简单:
# Gemfile
source 'https://rubygems.org'
gem 'activerecord'
gem 'require_all'
gem 'pg'
我有一个 rakefile.rb 来控制各种任务(编辑以删除不相关的项目):
# rakefile.rb
require "active_record"
require "require_all"
require "yaml"
namespace :db do
env = 'development' || ENV['env']
db_config = YAML::load(File.open('config/database.yml'))[env]
db_config_admin = db_config.merge({'database' => 'postgres', 'schema_search_path' => 'public'})
...
desc "Seed the database"
task :seed do
ActiveRecord::Base.establish_connection(db_config_admin)
require_all 'models/*.rb'
require_relative 'db/seeds.rb'
end
...
end
如前所述,rakefile 中的所有其他任务都可以正常工作——只有 :seed
任务会打嗝。我相当有信心它与脚本与数据库交互的方式有关。
这是种子文件:
# db/seeds.rb
Application.create({
ApplicationNumber: 1,
AccountNumber: 1234,
ApplVer: 1,
CreateDateTime: "1/2/2018",
ExpirationDateTime: "1/5/2019",
ApplicationStatus: "In process",
ApprovedAmount: 1992.92,
AcceptedAmount: 92.92,
HomeAddressLine1: "1 Main Street",
HomeAddressLine2: "",
HomeAddressCity: "Thibodaux",
HomeAddressState: "LA",
HomeAddressZipCode: "12345"
})
最后,我有一个应用程序的简单模型,seeds.rb 文件引用了它:
# models/application.rb
class Application < ActiveRecord::Base
end
现在上面提到的错误消息表明“关系“应用程序”不存在”......但是当我运行消息中引用的相同 SQL 时,直接在 pgAdmin 中,这就是我得到的(格式化的道歉 - 不能'弄清楚如何有效地将它放入表格中):
attname|format_type|pg_get_expr|attnotnull|atttypid|atttypmod|collname|comment
id|integer|nextval('applications_id_seq'::regclass)|t|23|-1||
ApplicationNumber|integer||f|23|-1||
AccountNumber|integer||f|23|-1||
ApplVer|integer||f|23|-1||
CreateDateTime|timestamp without time zone||f|1114|-1||
ExpirationDateTime|timestamp without time zone||f|1114|-1||
ApplicationStatus|character varying||f|1043|-1||
ApprovedAmount|numeric||f|1700|-1||
AcceptedAmount|numeric||f|1700|-1||
HomeAddressLine1|character varying||f|1043|-1||
HomeAddressLine2|character varying||f|1043|-1||
HomeAddressCity|character varying||f|1043|-1||
HomeAddressState|character varying||f|1043|-1||
HomeAddressZipCode|character varying||f|1043|-1||
FileCreatedDate|timestamp without time zone||f|1114|-1||
created_at|timestamp without time zone||t|1114|-1||
updated_at|timestamp without time zone||t|1114|-1||
所以底线似乎是:
- 脚本已正确配置以与 PG 一起使用 - 至少在创建、删除和迁移数据库方面
- rake db:seed
任务失败
- 命令行错误消息中报告的 SQL 在通过 pgAdmin 运行时工作正常
我不确定为什么会这样。它可能与分配给 database.yml
.. 中的数据库的 acmecorpapp
用户可用的权限有关吗?
最佳答案
请检查文件 rakefile.rb
。看来你合并了错误的数据库。用于开发的数据库是 acmecorp_dev
,您已经在 db_config
中拥有它。我认为您不必再合并 database
了。试试这个:
require "active_record"
require "require_all"
require "yaml"
namespace :db do
env = 'development' || ENV['env']
db_config = YAML::load(File.open('config/database.yml'))[env]
db_config_admin = db_config.merge({ 'schema_search_path' => 'public'})
...
desc "Seed the database"
task :seed do
ActiveRecord::Base.establish_connection(db_config_admin)
require_all 'models/*.rb'
require_relative 'db/seeds.rb'
end
...
end
关于没有 Rails 的 Ruby/Postgres ActiveRecord : Undefined Table ERROR: relation "..." does not exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47318575/