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
create_table :features, :id => false do |t| t.integer :id, :limit => 8 ...... end