为什么“execute”返回到 shell,而不是直接将 SQL 发送到我的 Postgres 服务器?
我有一个迁移,它使用 execute
将原始 SQL 发送到 Postgres 后端。
class TestExecuteMethod < ActiveRecord::Migration
def self.up
execute ('SELECT 1;')
end
def self.down
end
end
execute
似乎没有访问我的数据库,而是访问了 shell:
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:migrate
== TestExecuteMethod: migrating ==============================================
++ executing: SELECT 1;
sh: SELECT: command not found
++ [FAIL]
== TestExecuteMethod: migrated (0.0041s) =====================================
** Invoke db:schema:dump (first_time)
** Invoke environment
** Execute db:schema:dump
但是,当我这样做时,
ActiveRecord::Base.connection.execute(sql)
它会按预期工作。
这是为什么呢?
我正在使用 Rails 3.0.9 和 pg
gem。
I have a migration which is using execute
to send raw SQL to the Postgres backend.
class TestExecuteMethod < ActiveRecord::Migration
def self.up
execute ('SELECT 1;')
end
def self.down
end
end
Instead of going to my database, it seems execute
is going to the shell:
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:migrate
== TestExecuteMethod: migrating ==============================================
++ executing: SELECT 1;
sh: SELECT: command not found
++ [FAIL]
== TestExecuteMethod: migrated (0.0041s) =====================================
** Invoke db:schema:dump (first_time)
** Invoke environment
** Execute db:schema:dump
But, when I instead do
ActiveRecord::Base.connection.execute(sql)
It works as expected.
Why is this?
I'm using Rails 3.0.9 and the pg
gem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将其添加到您的迁移中:
然后将执行调用包装在如下所示的块中:
并粘贴跟踪输出的前几行。我怀疑在数据库适配器可以接收执行调用之前,有什么东西正在拦截它。
通常我建议使用类似的东西
,但在这种情况下可能不起作用,因为涉及 method_missing 。
Add this to your migration:
Then wrap your execute call in a block like this:
and paste the first few lines of trace output. I suspect something is intercepting the execute call before the db adapter can receive it.
Usually I'd recommend using something like
but that probably won't work in this case because method_missing is involved.
乍一看,execute 实际上是一个 shell exec,而不是一个 db exec。
老实说,我通常会在迁移中使用执行 - 这似乎有效,即
First glance is that execute is actually a shell exec - not a db exec.
I will be honest, I normally do use execute inside migrations - and that seems to work, i.e.