【问题标题】:PostgreSQL ERROR: invalid input syntax for integerPostgreSQL 错误:整数的输入语法无效
【发布时间】:2016-02-25 19:12:07
【问题描述】:

这是我第一次将 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 文件的 sn-p:

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

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

【问题讨论】:

  • 那不是 csv。 csv 每行有一条记录。您有一条记录分布在多行中。
  • 你建议使用什么?
  • csv 是其他所有格式都失败时的最后手段,即使那样你也应该三思而后行。几乎可以使用其他任何东西,例如xml - 它有处理内部数据转义的明确规定,否则会导致无效的 xml 文档。
  • 正如 Marc 所说,它应该是每行一条记录。如果您打算使用 COPY 命令,您还必须记住创建一个没有 ID 列的表,否则您必须在每行中放置一个唯一的序号值以适应复制时的 ID 列。
  • @MarcB 如何导入 xml 文件?我从本地机器上的 CSV 开始,效果很好,使用 /lib/tasks/import.rake 文件导入文章数据。我假设这不是 Rails 生产站点的工作方式?

标签: ruby-on-rails postgresql csv heroku


【解决方案1】:

看起来你可能在表中定义了一个整数字段,postgres 试图存储字符串值“员工人事文件”。检查表中未在 CSV 中定义的其他字段。

Header 用于指定 CSV 有一个标题行,以便在加载过程中可以忽略它,而不是作为将数据导入哪些字段的规范。

如果您正在开发 Rails 应用程序并使用迁移,您可能有一个定义为整数的 id 字段。

【讨论】:

    【解决方案2】:

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

    例如。 create_table :products, id: false do |t|

    这将省略 ID 列。然后,一旦您将所有记录的格式正确且每行有一条记录,您就可以使用 /COPY 命令将它们复制到 postgres 中。然后,您可以在事后重新添加一个 ID 列。

    【讨论】:

    • 我应该在哪里创建create_table :products, id: false do |t|?我在上面评论了我用于开发数据库的方法
    • 使用postgres的copy命令时,需要在数据库中已经有一张表。您要将此信息放入的表的名称是什么?给我你需要的所有列以及它们的数据类型,例如字符串或整数等,我会为你输入。
    • 表名为Article
    • 好的:在你的 rails 应用程序内的命令行上执行 > rails g model 文章标题:字符串正文:字符串 完成后告诉我
    • 我有文章模型。在尝试将 csv 导入 heroku 之前,我用 Article 做了一个脚手架
    【解决方案3】:

    我能够使用 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-05
      • 1970-01-01
      • 2014-07-17
      • 2020-05-05
      • 1970-01-01
      • 2012-12-21
      • 2020-04-27
      相关资源
      最近更新 更多