`
fsword
  • 浏览: 164709 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

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类型。
绕开的办法有两种:
  • 修改 activerecord-2.3.8/lib/active_record/connection_adapters/connection_adapters/mysql_adapter.rb 中的 NATIVE_DATABASE_TYPES 常量
  • 不设置主键,手工指定 id -

    create_table :features, :id => false do |t|
      t.integer :id, :limit => 8
      ......
    end

0
0
分享到:
评论
2 楼 头疼项目 2013-06-09  
wahte?
1 楼 头疼项目 2013-06-09  
[color=blue][/color]

相关推荐

Global site tag (gtag.js) - Google Analytics