PostgreSQL错误:整数的输入语法无效

时间:2016-02-25 19:12:07

标签: ruby-on-rails postgresql csv heroku

这是我第一次将CSV文件复制到我的Rails 4 Heroku应用程序 按照以下步骤收到错误消息。

从命令行收到的消息:

ERROR:  invalid input syntax for integer: "Employee personnel files"
CONTEXT:  COPY articles, line 29, column id: "Employee personnel files"

使用此命令:

PGPASSWORD=PWHERE psql -h HOSTHERE -U USERHERE DBNAMEHERE -c "\copy articles FROM 'lib/articles' WITH CSV HEADER;"

以下是CSV文件的摘录:

 line 1 "Title","Body"
 line 2 "Employee personnel files","
    As an HR professional you are no stranger to paperwork. It seems that for every employment action - applying, interviewing, hiring, disciplining, on and on - there is a specific form that needs to be filled out. Making sure you complete the paperwork properly is only half the battle though. Once you finish completing a form, you are faced with a whole new issue: what to do with it.  Being the smarty that you are, you know that proper documentation is key in protecting your company in the unfortunate case of a lawsuit, but knowing what needs to be kept where and for how long and who can see it can be kind of tricky. Let's take a minute to go over the basics.

...

 line 29 Looking for more sample polices and important forms? Click here to gain access."

有关缺少什么的任何建议?

3 个答案:

答案 0 :(得分:0)

似乎你可能在表中定义了一个字段,postgres试图存储字符串值"员工人事档案"。请在表格中查看CSV中未定义的其他字段。

Header用于指定CSV具有标题行,以便在加载过程中可以忽略它,而不是指定要将数据导入的字段。

如果您正在开发rails应用程序并使用迁移,则可能会将id字段定义为整数。

答案 1 :(得分:0)

首先,确保您正在创建您的表,这将包含此信息,但没有ID列。 ID列是由rails自动创建的,除非您计划向每行添加数字,否则将成为问题。然后,您可以在事实之后添加ID列以使其正确。

实施例。 create_table :products, id: false do |t|

这将留下ID列。然后,一旦你正确格式化了所有记录,每行有一个记录,你就可以使用/ COPY命令将它们复制到postgres中。然后,您可以在事后添加ID列。

答案 2 :(得分:0)

我能够使用heroku run rake db:seed命令和/ lib / tasks中的import.rake文件为我的heroku数据库播种。

    require 'csv'

    namespace :import do

      desc 'An optional description for what the task does'
      task :articles => :environment do
            CSV.foreach("lib/articles.csv", headers: true, encoding: "UTF-8") do |row|
            Article.create!(
              title: row["Title"], 
              body: row["Body"], 
            )
          end
      end
    end