我有一个想要通过 ActiveRecord 执行的 SQL 转储。我正在尝试这个:
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 数据库连接。我正在使用 PostgreSQL 9.2 。