简体   繁体   中英

SQL query does not work when executing from Rails console

I am trying to run the following line in my Rails console:

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:

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'`

However, when I run this exact query in Sequel Pro, the query runs perfectly fine. 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?

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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