简体   繁体   中英

When I run rails db:migrate the migration doesn't fully work

class CreateMessages < ActiveRecord::Migration[5.2]
  def change
    create_table :messages do |t|
      t.text :body
      t.integer :user_id
      t.timestamps
    end
  end
end

After running rails db:migrate my schema looks like this...

ActiveRecord::Schema.define(version: 2020_03_20_063104) do

  create_table "messages", force: :cascade do |t|
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "users", force: :cascade do |t|
    t.string "username"
    t.string "password_digest"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

end

I am wondering where the t.text "body" is and where the t.integer "user_id" is and why it isn't showing up in my schema under messages table.

I have checked migration status and all migrations have been ran.

If you ran something like this in your migration file

class CreateMessages < ActiveRecord::Migration[5.2]
  def change
    create_table :messages do |t|
      t.timestamps
    end
  end
end

Messages table is created and after this you can't create another migration with create_table :messages . Like @Marek Lipka wrote on comments. Either you need to rollback your CreateMessages migration and chance file and run your migration again. Or you need to write another migration to change existing table like this.

class AddBodyAndUserIdToMessages < ActiveRecord::Migration[5.2]
  def change
    add_column :messages, :body, :text
    add_column :messages, :user_id, :integer
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM