Rails Migration Generator Does Not Generate Columns

Low sleep is probably missing something trivial, but ...

Based on various readings of the documents, I thought that this would lead to a migration with table and column declarations enabled ...

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

However, the result ...

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

  def self.down
  end
end

Why are there no tables or columns?

+3
source share
3 answers

The command script/generate migrationdoes not create columns in new tables.

Only if you want to add, for example, a column to an existing table, you can pass a new column as an argument:

script/generate migration add_text_to_question question_text:string

For what you are trying to achieve, you need to create a new model:

script/generate model Question ordinal_label:string question_text:string

This will result in migration as follows:

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
+5
source

It should be

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

, unit test , . script/generate migrate , .

$ script/generate migration add_question_text_to_question question_text:string
+3

, .

script/generate migration AddLabelToQuestion label:string

or to create a new model you use your operator above, but replace the carry for the model.

0
source

Source: https://habr.com/ru/post/1723163/


All Articles