newUserNameHere
newUserNameHere

Reputation: 17971

How to get unique_checks=0 working with ruby active record

If I make build an insert query using the unique_checks=0 options specified in this documentation and then run it with: ActiveRecord::Base.connection.execute(query)

I get an error (see below). However, if I copy the query contained in that same error and run it without any changes in mysql it works just fine. So the query works, just something about how active record is running it is breaking it.

I must be missing some option in order to get this working query to work through ActiveRecord::Base.connection.execute Any help would be most appreciated!

#Error Message
=> #<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 'SET unique_checks=0;
INSERT INTO listings (cat, sub_cat, name, address, city, st' at line 2: SET autocommit=0;
SET unique_checks=0;
INSERT INTO listings (cat, sub_cat, name, address, city, state, zip, phone, fax) VALUES("Accountants","Tax Return Preparation","H&R Block","1 Mohegan Sun Blvd","Uncasville","CT","06382","8603831139",NULL),("Accountants","Tax Return Preparation","Sigler Accounting Service","12 Case St - Ste 304","Norwich","CT","06360","8608877555","8608878927");
SET unique_checks=1;
COMMIT;>

#Working SQL Query I stripped out of above error message
SET autocommit=0;
SET unique_checks=0;
INSERT INTO listings (cat, sub_cat, name, address, city, state, zip, phone, fax) VALUES("Accountants","Tax Return Preparation","H&R Block","1 Mohegan Sun Blvd","Uncasville","CT","06382","8603831139",NULL),("Accountants","Tax Return Preparation","Sigler Accounting Service","12 Case St - Ste 304","Norwich","CT","06360","8608877555","608878927");
SET unique_checks=1;
COMMIT;

Upvotes: 0

Views: 96

Answers (1)

spickermann
spickermann

Reputation: 106802

Unfortunately ActiveRecord::Base does not allow multiple queries in a single execute. This is a security feature and you cannot bypass this.

Upvotes: 1

Related Questions