gpt4 book ai didi

ruby-on-rails - rake 数据库 :seed unable to read CSV

转载 作者:行者123 更新时间:2023-11-29 12:20:33 25 4
gpt4 key购买 nike

我打算从 CSV 文件填充我的 PostgreSQL 数据库,但是当我执行 rake db:seed 时,我得到了这个错误:

rake aborted!
ActiveRecord::StatementInvalid: PG::InsufficientPrivilege: ERROR: could not open file "/home/spondbob/projects/rhsystem/db/data/doctors.csv" for reading: Permission denied
: COPY doctors(
name,
address,
phone,
field_id,
is_active,
email,
encrypted_password,
reset_password_token,
reset_password_sent_at,
remember_created_at,
sign_in_count,
current_sign_in_at,
last_sign_in_at,
current_sign_in_ip,
last_sign_in_ip)
FROM '/home/spondbob/projects/rhsystem/db/data/doctors.csv' WITH DELIMITER ',' CSV HEADER
/home/spondbob/.rvm/gems/ruby-2.1.5@rhsystem/gems/activerecord-4.1.8/lib/active_record/connection_adapters/postgresql/database_statements.rb:128:in `async_exec'

...

我不确定为什么它没有读取权限,我已经确定权限存在并且数据库连接已建立。这是文件权限:

$ ls -l db/data                     
total 56
-rwxr-xr-x 1 spondbob users 775 Dec 17 12:46 doctors.csv
...

还有我的第一个表的 seeds.rb:

connection = ActiveRecord::Base.connection()
connection.execute("COPY doctors(
name,
address,
phone,
field_id,
is_active,
email,
encrypted_password,
reset_password_token,
reset_password_sent_at,
remember_created_at,
sign_in_count,
current_sign_in_at,
last_sign_in_at,
current_sign_in_ip,
last_sign_in_ip)
FROM '#{Rails.root.join('db','data')}/doctors.csv' WITH DELIMITER ',' CSV HEADER")

感谢任何帮助。谢谢。

最佳答案

为此创建一个 rake 任务:

desc "Imports a CSV file into an ActiveRecord table"
task :csv_import, :filename, :model, :needs => :environment do |task,args|
lines = File.new(args[:filename]).readlines
header = lines.shift.strip
keys = header.split(',')
lines.each do |line|
params = {}
values = line.strip.split(',')
keys.each_with_index do |key,i|
params[key] = values[i]
end
Module.const_get(args[:model]).create(params)
end
end

然后像这样使用它:

rake csv_import file.csv model

关于ruby-on-rails - rake 数据库 :seed unable to read CSV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27517277/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com