问题
Since Rails 2.1, if you define a new column in a migration with the type set to :integer and the :limit set to 5 or more, the column actually created in your MySQL database will be of type BigInt. That's perfect.
But I cannot figure out how to create a table with a BigInt primary key.
Any clues?
回答1:
I just stumbled upon this plugin: it seems to answer this very question.
回答2:
This works in rails 3 not sure if it would work in rails 2.
Throughout my app I needed my primary keys to be bigint unsigned. What I ended up doing was putting in my config/environment.rb
require 'active_record/connection_adapters/mysql2_adapter'
ActiveRecord::ConnectionAdapters::Mysql2Adapter::NATIVE_DATABASE_TYPES[:primary_key] =
"BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY".freeze
This lets rails automatically create id's as BigInts. When I do a refrence from another table I do the following
t.column :product_id, 'BIGINT UNSIGNED'
来源:https://stackoverflow.com/questions/313136/how-can-i-define-a-bigint-primary-key-with-rails-2-1-and-mysql