Adding a column before another in Rails

I want to put a column in front of my table, I know what you can do

add_column :customer, :first_name, after: :last_name 

but is there a way to do :before ?

+5
source share
2 answers

You can insert a column in front of the table using the :first parameter:

 add_column :table_name, :column_name, :column_type, first: true 

You can use :after to handle all other positioning cases.

+4
source

These, incidentally, are mysql-specific options.

https://github.com/rails/rails/blob/80e66cc/activerecord/lib/active_record/connection_adapters/mysql/schema_creation.rb#L50-L55 lists the available options, it seems that they only support before and first .

For what it's worth, PostgreSQL only supports adding columns to the end of the table.

Related questions: change table add ... before `code`?

+1
source

Source: https://habr.com/ru/post/1258850/


All Articles