簡體   English   中英

在Rails遷移中從命令行使用列修飾符

[英]Using column modifiers from the command line in Rails Migrations

我正在閱讀有關遷移的Rails指南。 它指出以下內容,我引用:

一些常用的類型修飾符可以直接在命令行上傳遞。 它們用花括號括起來,並遵循字段類型:

它提供了一個示例:

$ bin/rails generate migration AddDetailsToProducts
'price:decimal{5,2}' supplier:references{polymorphic}

Rails指南還提供了列修飾符(SQL約束)的列表:

limit 
Sets the maximum size of the string/text/binary/integer fields.

precision 
Defines the precision for the decimal fields, representing the total number of digits in the number.

scale 
Defines the scale for the decimal fields, representing the number of digits after the decimal point.

polymorphic 
Adds a type column for belongs_to associations.

null 
Allows or disallows NULL values in the column.

default 
Allows to set a default value on the column.

index 
Adds an index for the column.

因此,現在我想在命令行中使用以下幾個列修飾符,但它不會生成預期的遷移:

rails generate resource Employee first_name:string{limit,40, null,false} last_name:string{limit,40,null,false} birth_date:date sex:boolean salary:integer supervisor_id:integer{index,true} branch_id:integer{null,false,index,true}

結果:

class CreateEmployees < ActiveRecord::Migration[5.1]
  def change
    create_table :employees do |t|
      t.string{limit,40, :first_name
      t.string :null,false}
      t.stringlimit :last_name
      t.string40 :last_name
      t.stringnull :last_name
      t.stringfalse :last_name
      t.date :birth_date
      t.boolean :sex
      t.integer :salary
      t.integerindex :supervisor_id
      t.integertrue :supervisor_id
      t.integernull :branch_id
      t.integerfalse :branch_id
      t.integerindex :branch_id
      t.integertrue :branch_id

      t.timestamps
    end
  end
end

我究竟做錯了什么?

您使用的格式不太正確。 看一下rails g model --help來解釋如何使用修飾符。 這是您要查找的內容的修改后的版本,盡管它不能解決您感興趣的所有情況:

rails generate resource Employee first_name:string{40} last_name:string{40} birth_date:date sex:boolean salary:integer supervisor_id:integer:index branch_id:integer:index

這將生成以下內容:

class CreateEmployees < ActiveRecord::Migration
  def change
    create_table :employees do |t|
      t.string :first_name, limit: 40
      t.string :last_name, limit: 40
      t.date :birth_date
      t.boolean :sex
      t.integer :salary
      t.integer :supervisor_id
      t.integer :branch_id

      t.timestamps null: false
    end
    add_index :employees, :supervisor_id
    add_index :employees, :branch_id
  end
end

您將必須手動將null: false添加到所需的那些條目。

但是,如果Branch和Supervisor也是AR對象,則可以執行以下操作:

rails generate resource Employee first_name:string{40} last_name:string{40} birth_date:date sex:boolean salary:integer supervisor:references branch:references

生成以下內容:

class CreateEmployees < ActiveRecord::Migration
  def change
    create_table :employees do |t|
      t.string :first_name, limit: 40
      t.string :last_name, limit: 40
      t.date :birth_date
      t.boolean :sex
      t.integer :salary
      t.references :supervisor, index: true, foreign_key: true
      t.references :branch, index: true, foreign_key: true

      t.timestamps null: false
    end
  end
end

這可能是您正在尋找的更多

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM