免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2267 | 回复: 0
打印 上一主题 下一主题

migrate中使用bigint [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-11-14 16:26 |只看该作者 |倒序浏览
migrate中使用bigint





使用mysql时,integer最大是10位。如果想存储更大的数字就要使用BIGINT。比如现在QQ号已经有11位,很快就12位了。



Ruby代码
  1. 1.# activerecord-3.0.7/lib/active_record/connection_adapters/mysql_adapter.rb         
  2. 2.def type_to_sql(type, limit = nil, precision = nil, scale = nil)   
  3. 3.    return super unless type.to_s == 'integer'  
  4. 4.  
  5. 5.    case limit   
  6. 6.    when 1; 'tinyint'  
  7. 7.    when 2; 'smallint'  
  8. 8.    when 3; 'mediumint'  
  9. 9.    when nil, 4, 11; 'int(11)'  # compatibility with MySQL default   
  10. 10.    when 5..8; 'bigint'  
  11. 11.    else raise(ActiveRecordError, "No integer type has byte size #{limit}")   
  12. 12.    end  
  13. 13.end  
  14. # activerecord-3.0.7/lib/active_record/connection_adapters/mysql_adapter.rb      
  15. def type_to_sql(type, limit = nil, precision = nil, scale = nil)
  16.     return super unless type.to_s == 'integer'

  17.     case limit
  18.     when 1; 'tinyint'
  19.     when 2; 'smallint'
  20.     when 3; 'mediumint'
  21.     when nil, 4, 11; 'int(11)'  # compatibility with MySQL default
  22.     when 5..8; 'bigint'
  23.     else raise(ActiveRecordError, "No integer type has byte size #{limit}")
  24.     end
  25. end
复制代码
所以我们可以添加:limit => 5/6/7/8来得到一个bigint列。



Ruby代码
  1. 1.t.integer :qq, :limit => 8  
  2. t.integer :qq, :limit => 8
复制代码
如果想设置id为bigint,还要在create_table时传递:id => false,然后手动指定id列。



Ruby代码
  1. 1.def self.up     
  2. 2.    create_table :demo, :id => false do |t|     
  3. 3.    t.integer :id, :limit => 8   
  4. 4.end   
  5. def self.up  
  6.     create_table :demo, :id => false do |t|  
  7.     t.integer :id, :limit => 8
  8. end  
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP