migration integer limit option

本文详细介绍了SQL中不同字段类型的大小限制,例如tinyint、smallint等,并提供了具体的例子来展示如何在创建表时指定这些限制。


https://gist.github.com/stream7/1069589
:limit	    Numeric Type	Column Size	Max value
1	     tinyint	         1 byte	          127
2	     smallint	         2 bytes	32767
3	     mediumint	         3 byte	        8388607
nil, 4, 11    int(11)	         4 byte	       2147483647
5..8	      bigint	         8 byte	     9223372036854775807


关于sequence 字段
limit: 2 是两个字节,16 位,2 的 15 次方是 32767


class CreateTableSubjections < ActiveRecord::Migration[5.0]
  def change
    create_table :table_subjections do |t| 
      t.belongs_to :subject, index: true
      t.belongs_to :video, index: true
      t.string :title, limit: 40
      t.string :sub_title, limit: 40
      t.string :description, limit: 500
      t.integer :status, limit: 1
      t.integer :sequence, limit: 2

      t.timestamps
    end 
  end 
end

对于多态关联的表

class CreateSubjections < ActiveRecord::Migration[5.0]
  def change
    create_table :subjections do |t| 
      t.belongs_to :subject, index: true
      t.string :title, limit: 40
      t.string :sub_title, limit: 40
      t.string :description, limit: 500 
      t.string :subjectable_type, limit: 20
      t.integer :subjectable_id
      t.integer :status, limit: 1
      t.integer :sequence, limit: 2

      t.timestamps
    end 

    add_index :subjections, [:subjectable_type, :subjectable_id]
  end 
end


t.integer :subjectable_id
这个字段不需要设置limit,因为这个字段是用来做外键的,存储的是数据库的自增主键。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值