Rails迁移生成器不生成列

Eth*_*han 3 ruby ruby-on-rails

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

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

$ script/generate migration Question ordinal_label:string question_text:string
Run Code Online (Sandbox Code Playgroud)

但是,结果是......

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

  def self.down
  end
end
Run Code Online (Sandbox Code Playgroud)

为什么没有桌子或列?

rog*_*pvl 5

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

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

script/generate migration add_text_to_question question_text:string
Run Code Online (Sandbox Code Playgroud)

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

script/generate model Question ordinal_label:string question_text:string
Run Code Online (Sandbox Code Playgroud)

这将生成如下迁移:

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
Run Code Online (Sandbox Code Playgroud)