如何在Rails 5中的现有表中添加新列?

时间:2016-08-02 10:56:50

标签: ruby-on-rails ruby rails-migrations ruby-on-rails-5

我想在Rails 5中的一个表中添加一个新列。我最近使用以下方式重命名了一个列:

import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.Icon;



public class GameBoard extends JPanel implements ActionListener {

private JButton B1, B2, B3, B4, B5, B6, B7, B8, B9; 
private GameArray GArray; 
private boolean Player = false;
private int PlayerMark = 1;

GameBoard() {

    setLayout(new GridLayout(3, 3));

    B1 = new JButton();
    ...
    B9 = new JButton();

    SetGame();

    add(B1);
    ...
    add(B9);

    B1.addActionListener(this);
    ...
    B9.addActionListener(this);

}

public void SetGame() {

    GArray = new GameArray(this);

    DefaultText();
    DisableAll(true);

    Player = false; 
    PlayerMark = 1;
}

public void Reset() {

    SetGame(); 

}

public void actionPerformed(ActionEvent E) {

    ...

}

//Other methods

public void SetText(JButton Btn, boolean Play) {

    if (Play == true) {
        Btn.setText("O");

    } else if (Play == false) {

        Btn.setIcon(new ImageIcon(GameBoard.class.getResource("red.png")));

    }
  }
}

然后在rails g migration ChangeJobsTable

20160802104312_change_jobs_table.rb

然后

class ChangeJobsTable < ActiveRecord::Migration[5.0]
  def change
    rename_column :jobs, :skills, :skills1
  end
end

它工作正常,但现在如果我还要添加新列rails db:migrate ,我是否需要像这样做?

skills2

3 个答案:

答案 0 :(得分:12)

您忘记添加datatype,以下是更新后的迁移。

class ChangeJobsTable < ActiveRecord::Migration[5.0]
  def change
    add_column :jobs, :skills2, :string
  end
end

答案 1 :(得分:8)

你确实忘记了datatype。您也可以在将来通过控制台执行此操作:

rails g migration AddSkills2ToJobs skills2:string

答案 2 :(得分:0)

这对我有用,您可以在模式中进行验证

rails g migration add_skills2_to_ChangeJobsTable skills2:string

rake db:migrate