简体   繁体   中英

PostgreSQL ERROR: invalid input syntax for integer

This is my first time copying a CSV file to my Rails 4 Heroku app.
Following the steps below receives an error message.

Message received from command line:

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

Using this command:

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

Here's a snippet of the CSV file:

 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."

Any suggestions on what is missing?

It seems like you might have a field defined as an integer in the table that postgres is trying to store the string value "Employee personnel files". Check your table for additional fields that aren't defined in your CSV.

Header is used to specify that the CSV has a header row so that it can be ignored during the load process, not as a specification of what fields to import the data into.

If you're developing a rails application and using migrations you probably have an id field that's defined as an integer.

Firstly, make sure you are creating your table, that is going to house this information, that doesn't have an ID column. The ID column is automagically created by rails and will be a problem unless you plan on adding numbers to every row. You can then add an ID column after the fact to make it right.

Ex. create_table :products, id: false do |t|

This will leave off an ID column. Then, once you get all records formatted correctly in which there is one record per line, you will be ready to COPY them into postgres using the /COPY command. You can then add an ID column back in after the fact.

I was able to seed my heroku database with the heroku run rake db:seed command and this import.rake file in /lib/tasks.

    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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM