简体   繁体   English

从Rails控制台执行时,SQL查询不起作用

[英]SQL query does not work when executing from Rails console

I am trying to run the following line in my Rails console: 我试图在我的Rails控制台中运行以下行:

ActiveRecord::Base.connection.execute "DELETE FROM recommendations WHERE place_id=4000000001267 AND recommender_id=2728498;UPDATE IGNORE recommendations SET place_id=4000000001267 WHERE place_id=4000000564630 AND recommender_id=2728498;"

It is simply deleting an old record and rewriting another record for merging purposes. 它只是删除旧记录并重写另一条记录以进行合并。

Rails console returns the following: Rails控制台返回以下内容:

DELETE FROM recommendations WHERE place_id=4000000001267 AND recommender_id=2728498;UPDATE IGNORE recommendations SET place_id=4000000001267 WHERE place_id=4000000564630 AND recommender_id=2728498; ActiveRecord::StatementInvalid: Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UPDATE IGNORE recommendations SET place_id=4000000001267 WHERE place_id=40000005' at line 1: DELETE FROM recommendations WHERE place_id=4000000001267 AND recommender_id=2728498;UPDATE IGNORE recommendations SET place_id=4000000001267 WHERE place_id=4000000564630 AND recommender_id=2728498; from /Users/huynguyen/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.6/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:243:in DELETE FROM recommendations WHERE place_id=4000000001267 AND recommender_id=2728498;UPDATE IGNORE recommendations SET place_id=4000000001267 WHERE place_id=4000000564630 AND recommender_id=2728498; ActiveRecord::StatementInvalid: Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UPDATE IGNORE recommendations SET place_id=4000000001267 WHERE place_id=40000005' at line 1: DELETE FROM recommendations WHERE place_id=4000000001267 AND recommender_id=2728498;UPDATE IGNORE recommendations SET place_id=4000000001267 WHERE place_id=4000000564630 AND recommender_id=2728498; from /Users/huynguyen/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.6/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:243:in query'` DELETE FROM recommendations WHERE place_id=4000000001267 AND recommender_id=2728498;UPDATE IGNORE recommendations SET place_id=4000000001267 WHERE place_id=4000000564630 AND recommender_id=2728498; ActiveRecord::StatementInvalid: Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UPDATE IGNORE recommendations SET place_id=4000000001267 WHERE place_id=40000005' at line 1: DELETE FROM recommendations WHERE place_id=4000000001267 AND recommender_id=2728498;UPDATE IGNORE recommendations SET place_id=4000000001267 WHERE place_id=4000000564630 AND recommender_id=2728498; from /Users/huynguyen/.rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.6/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:243:in query'`

However, when I run this exact query in Sequel Pro, the query runs perfectly fine. 但是,当我在Sequel Pro中运行这个确切的查询时,查询运行完全正常。 Any idea what is going on? 知道发生了什么事吗?

Might it be connected to the fact AR's mysql adapter doesn't have multiple query execution on by default? 可能它连接到AR的mysql适配器默认情况下没有多个查询执行的事实?

A quick fix for this would be splitting the query into two distinct and execute them one after another. 对此的快速解决方法是将查询拆分为两个不同的并一个接一个地执行它们。 And wrapping them into a transaction, of course :) 并将它们包装成一个事务,当然:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM