使用小型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
现在上面提到的错误信息表明`关系&#34;应用程序&#34;不存在&#34; ...但是当我在消息中运行相同的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
我不确定为什么会这个问题。是否与acmecorpapp
中分配给数据库的database.yml
用户可用的权限有关?
答案 0 :(得分:2)
请检查档案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
答案 1 :(得分:0)
好的,在Chivorn Kouch上面的建议之后想出来了。解决方案需要额外的db_config变量,以及对:seed
任务的修改。
在rakefile.rb
:
# rakefile.rb
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' })
# add following line to create variable for the :seed task
db_config_seed = db_config.merge({ 'schema_search_path' => 'public' })
...
desc "Seed the database"
task :seed do
# change variable referenced in the connection line below
ActiveRecord::Base.establish_connection(db_config_seed)
require_all 'models/*.rb'
require_relative 'db/seeds.rb'
puts "Database seeded."
end
...
end
通过这些更改,命令全部按预期工作。