在Rails 4 / Postgres中对schema.rb中的列重新排序是否安全? [英] Is it safe to reorder columns in schema.rb for Rails 4/Postgres?

查看:77
本文介绍了在Rails 4 / Postgres中对schema.rb中的列重新排序是否安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开发和生产中使用带有Postgres 9.4的Running Rails 4。我有一张大桌子,随着迁移的发展,桌子已经越来越大。由于表上有许多不同的列,因此我想对事物进行重新排序,以使这些列更合理地分组。换句话说,某些列元素会根据它们捕获的信息自然地组合在一起。

Running Rails 4 with Postgres 9.4 in development and production. I've got a large table that has grown by migrations over time. Because of the many different columns on the table, I want to reorder things so that the columns are grouped more logically. In other words, some column elements naturally group together based on what information they capture.

我发现在以下位置使用进行了讨论:,以重新排序列(在SQL中使用 ALTER TABLE ),位于此堆栈溢出讨论。然后,我继续进行迁移,以完成此任务。运行迁移后,我注意到我的schema.rb文件没有更改。查看数据库中的列(开发中的postgres),那里也没有任何改变。

I found a discussion on using after: in a migration to reorder columns (using ALTER TABLE in SQL), at this stack overflow discussion. I then went ahead and set up a migration to do that. After running the migration I notice that my schema.rb file hasn't changed. Looking at the columns in the database (postgres on development), nothing has changed there either.

进一步的研究使我想到了 Postgres Wiki ,其中指出目前不支持更改Postgres中的列位置。

Further research led me to the Postgres wiki, which states there is currently no support for altering column position in Postgres.

这不是关键任务,但可以使生活更轻松。我的问题是:

This isn't mission critical, but it would make life easier. My question is:


  1. 如果我只是在 schema.rb 向上或向下归档以便根据需要定位它们,这会引起任何问题吗?由于我希望使用rake db:schema:load来配置任何新数据库,因此看来这不会破坏任何内容。此外,由于以前的迁移没有实质性的更改(只是它们输出到schema.rb的列的顺序),所有内容都应该在内部保持一致,不是吗?

  1. 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? Since I expect to provision any new database with rake db:schema:load, it wouldn't seem like that should break anything. Further, since the previous migrations aren't materially changed (just the order of columns they output to schema.rb) everything should be internally consistent, no?

如果那是个坏主意,我可以简单地回到以前的迁移中,在每个列元素中添加之后的语法::some_column ,以便它们正确设置schema.rb文件?

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?

我的假设是,当我使用种子数据从头开始重建生产数据库时,它将使用该架构结构,以按所需顺序正确创建表。目前,它不是一个真正的,具有最终用户的已部署数据库,因此似乎没有问题。只要不破坏任何内容,使用上面的选项#1似乎是最简单的解决方案。

My assumption is that when I rebuild my production database from scratch with seed data, it will use the schema structure to correctly create the table in the desired order. Right now it's not a real, deployed database with end users, so it doesn't seem like an issue. Using option #1 above seems like the easiest solution, as long as it doesn't break anything.

推荐答案


许多不熟悉Postgresql的人经常问它是否支持更改表中的
列位置。 当前没有如果要
更改列的位置,则必须重新创建表,或添加
新列并移动数据。
允许对列进行重新排序的想法
的职位不是Postgresql开发人员所反对的,更多是
a的情况,没有人上前去做这项工作。

参考


  • 如果我只是简单地上下移动 schema.rb 文件中的行,以便根据需要将
    定位,会不会引起任何问题?

  • 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?

否。它对 PG 中的列顺序不起作用。

no. It does nothing with column order in PG.


  • 如果这是一个坏主意,我可以简单地回到以前的迁移中,在每个列元素中添加之后的语法::some_column 以便它们正确设置schema.rb文件吗?

  • 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?

之后::some_column 如果使用 PG

这篇关于在Rails 4 / Postgres中对schema.rb中的列重新排序是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆