Is it safe to reorder columns in schema.rb for Rails 4/Postgres?

不羁岁月 提交于 2019-12-01 11:26:07

Many people new to postgresql often ask if it has support for altering column positions within a table. Currently it does not; if you want to change column positions, you must either recreate the table, or add new columns and move data. The idea of allowing re-ordering of column position is not one the postgresql developers are against, it is more a case where no one has stepped forward to do the work.

ref

  • If I simply move lines in the schema.rb file up or down in order to position them as desired, will that cause any problems?

no. It does nothing with column order in PG.

  • If that's a bad idea, can I simply go back into the earlier migrations, add the syntax for after: :some_column to each column element so they correctly set up the schema.rb file?

after: :some_column option does nothing with column order if you use PG

The order of your columns does not matter to Ruby or Rails.

As to whether it matters to any code you wrote, only you and your tests can answer that. If you're only using ActiveRecord and not doing any straight SQL that references column number, you should be fine.

As a workaround, you can use annotate to document your models in the code and then reorder the created comment there.

Of course, that will probably be overwritten by annotate once you run it again. And it will only make your life easier if you look at the table structure, but it won't help if you manually run SQL queries with SELECT * in your db.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!