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!
CodePudding user response:
alter table <name> rename to <new name>
takes a fully qualified table name, but the new name is just the table.
ALTER TABLE custom.students RENAME TO class_one_students;
You'd do this in a migration with rename_table
. Again, only pass the table name as the new name.
rename_table('custom.students', 'class_one_students')
Note that the new name implies this is a table of students for a single class and that there will be a class_two_students
, class_three_students
, etc. This should instead be handled with students
, classes
and a join table.
CodePudding user response:
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