From dfdf22c7d8a9e4776a0e3bfc130ef47aff911022 Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Wed, 27 Dec 2017 03:22:46 -0800 Subject: Fix PostgreSQL implementation of migration --- db/migrate/20171106151218_issues_moved_to_id_foreign_key.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'db') diff --git a/db/migrate/20171106151218_issues_moved_to_id_foreign_key.rb b/db/migrate/20171106151218_issues_moved_to_id_foreign_key.rb index 18c2b294c1e..6368e5bd9ce 100644 --- a/db/migrate/20171106151218_issues_moved_to_id_foreign_key.rb +++ b/db/migrate/20171106151218_issues_moved_to_id_foreign_key.rb @@ -15,7 +15,9 @@ class IssuesMovedToIdForeignKey < ActiveRecord::Migration self.table_name = 'issues' def self.with_orphaned_moved_to_issues - where('NOT EXISTS (SELECT true FROM issues WHERE issues.id = issues.moved_to_id)') + # Be careful to use a second table here for comparison otherwise we'll null + # out all rows that don't have id == moved_to_id! + where('NOT EXISTS (SELECT true FROM issues b WHERE issues.moved_to_id = b.id)') .where('moved_to_id IS NOT NULL') end end -- cgit v1.2.1