Search code examples
ruby-on-railsdatabase-migration

Rename Table Name rails


I have table name like this "custom.students" in rails Now I would like to change it as "custom.class_one_students"

I have tried multiple ways but nothing works like migration, sql and single quotation!


Solution

  • I have gone through this documentation and it seems like for the custom schema, it would be effective to use search_path for alteration in database.it works for me!!

      class ChangeStudentTable < ActiveRecord::Migration[5.2]
           def up
            execute(<<-SQL)
              SET search_path TO custom;
              ALTER TABLE custom.students RENAME TO custom.class_one_students;
              SET search_path TO "$user", public;
            SQL
          end
        end