rails不支持bigint类型的mysql主键
之前遇到过很多次,老是忘记,今天记录一下吧。
mysql可以支持int、tinyint、bigint等几种数据格式的字段,rails(准确说是activerecord)抽象了数据结构,统一使用integer表示整数,然后根据limit来决定使用哪种int:
# activerecord-2.3.8/lib/active_record/connection_adapters/abstract/schema_definitions.rbmodule 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 endend
但是这个做法仅限于普通字段,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