Error in migration - Error: You have an error in your SQL syntax; check the manual that corresponds

19 views
Skip to first unread message

Dmitriy

unread,
Jul 10, 2015, 9:07:26 AM7/10/15
to rubyonra...@googlegroups.com
Cant undestand what wrong with syntax sql file migration. please, help:

i run rake:db:migrate

error:

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 ')' at line 1: UPDATE photos SET votes_count = 0, rating = 0 WHERE id NOT IN ()/home/user/myapp/appmame/db/migrate/20131110162613_recalculate_photos_rating.rb:8:in `up'


class RecalculatePhotosRating < ActiveRecord::Migration
 
def up
   
if Vote.where("subject_type='Photo' and rating > 1").count > 0
     
ActiveRecord::Base.connection.execute "DELETE FROM votes WHERE subject_type='Photo' AND rating <= 5"
     
ActiveRecord::Base.connection.execute "UPDATE votes SET rating=1 WHERE subject_type='Photo'"
   
end
    pids
= Vote.where(subject_type: 'Photo').pluck(:subject_id).uniq
   
ActiveRecord::Base.connection.execute "UPDATE photos SET votes_count = 0, rating = 0 WHERE id NOT IN (#{pids.join(', ')})"
   
ActiveRecord::Base.connection.execute "UPDATE photos SET rating=(SELECT COUNT(*) FROM votes WHERE votes.subject_type='Photo' AND subject_id=photos.id) WHERE id IN (#{pids.join(', ')})"
   
ActiveRecord::Base.connection.execute "UPDATE photos SET votes_count=(SELECT COUNT(*) FROM votes WHERE votes.subject_type='Photo' AND subject_id=photos.id) WHERE id IN (#{pids.join(', ')})"
 
end

 
def down
 
end
end



Message has been deleted

Dmitriy

unread,
Jul 10, 2015, 11:05:04 AM7/10/15
to rubyonra...@googlegroups.com
i inserted
pids = Vote.where(subject_type: 'Photo').pluck(:subject_id).
uniq
    unless pids.empty?

but again same error

пятница, 10 июля 2015 г., 17:27:12 UTC+3 пользователь Elizabeth McGurty написал:
If I am reading your error message correctly:

UPDATE photos SET votes_count = 0, rating = 0 WHERE id NOT IN ()
Looks like your IN Operator is empty.  Pretty sure that will throw an error.  If I am correct, I think that you should check the return on pids.
Otherwise, thanks for this offering.... I learned a lot from it. 



Dmitriy

unread,
Jul 10, 2015, 11:30:56 AM7/10/15
to rubyonra...@googlegroups.com
now work. thank you

пятница, 10 июля 2015 г., 18:05:04 UTC+3 пользователь Dmitriy написал:
Reply all
Reply to author
Forward
Message has been deleted
0 new messages