Rails 4:向现有表添加多个列

时间:2022-07-05 07:32:23

I know how to add one column to an existing table. Now I have to add many columns to an existing table. Is there a shorter way for:

我知道如何向现有表添加一个列。现在我必须向现有的表添加许多列。有没有更短的方法?

add_col1_col2_col3_col4_.._coln_to_tables col1:integer col2:integer etc...

Do I have to do the above for ALL the additional columns I have to add?

我是否需要为所有附加的列做上述操作?

6 个解决方案

#1


33  

No not necessary. You can do

不是必要的。你可以做

Assuming TableName is user

假设表名是用户

rails g migration AddColumnsToUser col1:integer col2:integer .. etc.

#2


14  

Here's a good resource on ActiveRecord:Migrations which lists all the commands you can use to manipulate your databases. You can also do the task this way:

这是ActiveRecord的一个好资源:它列出了可以用来操作数据库的所有命令。你也可以这样做:

rails g migration AddMoreColumnsToModel

rails迁移g AddMoreColumnsToModel

Then open the migration file and put:

然后打开迁移文件并放置:

def change
  add_column :table, :new_column, :type
  # add as many columns as you need 
end

If you wanted to do what Maxd suggests, having literally 100 columns of the same type auto-create, his code is a good idea.

如果您想按照Maxd的建议进行操作,即拥有100列相同类型的自动创建,那么他的代码是一个好主意。

#3


3  

Just create migration and generate these columns i.e.:

只需创建迁移并生成这些列,例如:

class ChangeTables < ActiveRecord::Migration
  def change
    change_table :tables do |t|
      100.times do |i|
        t.integer :"column_#{i}"
      end
    end
  end
end

#4


2  

This migration file can be done in a loop. But do you really want to do this? It does not look correct to create such a heavy model to hold everything.

这个迁移文件可以在循环中完成。但你真的想这么做吗?创建一个如此沉重的模型来容纳所有东西看起来不正确。

#5


0  

Similar to above answers but for understanding purpose hope below naming convention is good.

类似于以上的答案,但为了理解目的,希望下面的命名约定是好的。

rails g migration add_first_column_and_second_column_to_model first_column:string second_column:string

#6


0  

command to create new model and table with columns:

命令创建具有列的新模型和表:

rails g model ModelName col_name1:string col_name2:integer col_name3:text ...

command to add more columns under existing table:

命令在现有表下添加更多列:

rails g migration AddColumnToModelName col_name4:string col_name5:integer ...

At last, run migration by command:

最后,通过命令运行迁移:

rake db:migrate

#1


33  

No not necessary. You can do

不是必要的。你可以做

Assuming TableName is user

假设表名是用户

rails g migration AddColumnsToUser col1:integer col2:integer .. etc.

#2


14  

Here's a good resource on ActiveRecord:Migrations which lists all the commands you can use to manipulate your databases. You can also do the task this way:

这是ActiveRecord的一个好资源:它列出了可以用来操作数据库的所有命令。你也可以这样做:

rails g migration AddMoreColumnsToModel

rails迁移g AddMoreColumnsToModel

Then open the migration file and put:

然后打开迁移文件并放置:

def change
  add_column :table, :new_column, :type
  # add as many columns as you need 
end

If you wanted to do what Maxd suggests, having literally 100 columns of the same type auto-create, his code is a good idea.

如果您想按照Maxd的建议进行操作,即拥有100列相同类型的自动创建,那么他的代码是一个好主意。

#3


3  

Just create migration and generate these columns i.e.:

只需创建迁移并生成这些列,例如:

class ChangeTables < ActiveRecord::Migration
  def change
    change_table :tables do |t|
      100.times do |i|
        t.integer :"column_#{i}"
      end
    end
  end
end

#4


2  

This migration file can be done in a loop. But do you really want to do this? It does not look correct to create such a heavy model to hold everything.

这个迁移文件可以在循环中完成。但你真的想这么做吗?创建一个如此沉重的模型来容纳所有东西看起来不正确。

#5


0  

Similar to above answers but for understanding purpose hope below naming convention is good.

类似于以上的答案,但为了理解目的,希望下面的命名约定是好的。

rails g migration add_first_column_and_second_column_to_model first_column:string second_column:string

#6


0  

command to create new model and table with columns:

命令创建具有列的新模型和表:

rails g model ModelName col_name1:string col_name2:integer col_name3:text ...

command to add more columns under existing table:

命令在现有表下添加更多列:

rails g migration AddColumnToModelName col_name4:string col_name5:integer ...

At last, run migration by command:

最后,通过命令运行迁移:

rake db:migrate