Error in migration - Error: You have an error in your SQL syntax; check the manual that corresponds
Error in migration - Error: You have an error in your SQL syntax; check the manual that corresponds
无法理解语法 sql 文件迁移有什么问题。请帮助:
我运行耙db:migrate
错误:
Mysql2::错误:您的 SQL 语法有误;检查与您的 MySQL 服务器版本对应的手册,了解在第 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:在“向上”
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
您必须检查 pids
查询 returns 是否为空结果:
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
unless pids.empty?
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
end
def down
end
end
无法理解语法 sql 文件迁移有什么问题。请帮助:
我运行耙db:migrate
错误:
Mysql2::错误:您的 SQL 语法有误;检查与您的 MySQL 服务器版本对应的手册,了解在第 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:在“向上”
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
您必须检查 pids
查询 returns 是否为空结果:
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
unless pids.empty?
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
end
def down
end
end