Rails迁移生成器不生成列

时间:2009-11-17 21:31:54

标签: ruby-on-rails ruby

低睡眠因此可能会遗漏一些微不足道的东西,但是......

基于各种文档读数,我认为这会生成包含表和列声明的迁移...

$ script/generate migration Question ordinal_label:string question_text:string

然而,结果是......

class Question < ActiveRecord::Migration
  def self.up
  end

  def self.down
  end
end

为什么没有表格或列?

3 个答案:

答案 0 :(得分:5)

script/generate migration命令不会在新表上创建列。

只有当您希望将列添加到现有表时,才能将新列作为参数传递:

script/generate migration add_text_to_question question_text:string

对于您要实现的目标,您必须创建一个新模型:

script/generate model Question ordinal_label:string question_text:string

这将生成如下的迁移:

class CreateQuestions < ActiveRecord::Migration
    def self.up
      create_table :questions do |t|
        t.string  :ordinal_label
        t.string  :question_text
        t.timestamps
      end
    end

    def self.down
      drop_table :questions
    end
  end

答案 1 :(得分:3)

那应该是

$ script/generate model Question ordinal_label:string question_text:string

您最终还将获得模型,单元测试和夹具。 script/generate migrate会在现有表格中添加一列,但不会向新表格添加。

$ script/generate migration add_question_text_to_question question_text:string

答案 2 :(得分:0)

如果要修改模型,则需要动词。

script/generate migration AddLabelToQuestion label:string

或要生成新模型,您可以使用上面的语句,但将迁移替换为模型。

相关问题