rails不支持bigint类型的mysql主键

之前遇到过很多次,老是忘记,今天记录一下吧。

mysql可以支持int、tinyint、bigint等几种数据格式的字段,rails(准确说是activerecord)抽象了数据结构,统一使用integer表示整数,然后根据limit来决定使用哪种int:

# activerecord-2.3.8/lib/active_record/connection_adapters/abstract/schema_definitions.rb
module ActiveRecord
module ConnectionAdapters #:nodoc:
# An abstract definition of a column in a table.
class Column
def initialize(name, default, sql_type = nil, null = true)
@name, @sql_type, @null = name, sql_type, null
@limit, @precision, @scale = extract_limit(sql_type), extract_precision(sql_type), extract_scale(sql_type)
@type = simplified_type(sql_type)
@default = extract_default(default)

@primary = nil
end
......
def simplified_type(field_type)
case field_type
when /int/i
:integer
when /float|double/i
:float
when /decimal|numeric|number/i
extract_scale(field_type) == 0 ? :integer : :decimal
when /datetime/i
:datetime
when /timestamp/i
:timestamp
when /time/i
......
end
end
end
end
end
end

但是这个做法仅限于普通字段,activerecord中对于主键存在大量硬编码,因此我们就不能通过改变主键的limit的方式设置int类型。
绕开的办法有两种:
[list]
[*] 修改 activerecord-2.3.8/lib/active_record/connection_adapters/connection_adapters/mysql_adapter.rb 中的 NATIVE_DATABASE_TYPES 常量
[*] 不设置主键,手工指定 id -
[/list]

create_table :features, :id => false do |t|
t.integer :id, :limit => 8
......
end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值