我有一个SQL,我希望通过ActiveRecord执行这个转储。我在尝试这个:
ActiveRecord::Base.connection.execute(File.read(sql_seeds))
但我知道这个错误:
rake aborted!
PG::Error: ERROR: syntax error at or near "1"
LINE 18: 1 Shanghai 2012-12-20 10:31:31.350111 2012-12-20 10:31:31.35...
在SQL脚本的这一行:
COPY locations (id, description, created_at, updated_at) FROM stdin;
1 Shanghai 2012-12-20 10:31:31.350111 2012-12-20 10:31:31.350111
一些迹象:
ActiveRecord::Base.connection.execute("\\i #{sql_seeds}")
无法工作,因为\i
是psql
命令(谢谢@JiříPospíšil和@RichardHuxton)%X( psql -U #{user} -H #{host} -P #{pass} ... )
,因为我会使用已经存在的ActiveRecord数据库连接。发布于 2013-01-09 05:31:39
问题是COPY FROM stdin
;使用pg_dump --inserts
进行编辑解决了这个问题。
发布于 2013-01-09 04:11:34
问题在于,\i
是psql
的命令,而不是postgresql
的命令。您不能在查询中直接使用它。而且,我也不知道为什么execute
方法会在导入工作时失败。
我认为您将不得不执行,即使这意味着创建另一个db连接。注意,您不需要直接组装psql
命令,您可以使用db
命令:
rails db < seeds.sql
通过这种方式,您可以在当前环境中重用config/database.yml
中的连接信息。
https://stackoverflow.com/questions/14234133
复制相似问题