【问题标题】:What is the Rails Migration for creating a table?创建表的 Rails 迁移是什么?
【发布时间】:2014-01-13 17:09:12
【问题描述】:

什么是用于创建表的 Rails 迁移?

我试过这个迁移生成器:

$ rails g migration CreateQuestions user:references question_title:string question_text:text approved:boolean

这会创建一个似乎缺少通常的 idtimestamps 字段的迁移。我搞砸了我的发电机吗?

class CreateQuestions < ActiveRecord::Migration
  def change
    create_table :questions do |t|
      t.references :user, index: true
      t.string :question_title
      t.text :question_text
      t.boolean :approved
    end
  end
end

【问题讨论】:

  • 是否有您不想生成模型(创建表)的特定原因?
  • 啊对,所以我应该这样做rails g model Questions user:references question_title:string question_text:text approved:boolean

标签: ruby-on-rails ruby-on-rails-4 rails-activerecord database-migration rails-migrations


【解决方案1】:
  1. "A primary key column called id will also be added implicitly, as it's the default primary key for all Active Record models."

  2. 如果您想添加 timestamps,请将其添加到您的迁移文件中:

    t.timestamps
    

【讨论】:

  • 有没有办法从生成器中包含它?
  • 看起来只是在生成器中有“时间戳”
猜你喜欢
  • 1970-01-01
  • 2012-10-01
  • 2019-03-30
  • 2018-02-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-20
相关资源
最近更新 更多