How can I define a BigInt primary key with Rails 2.1 and MySQL?

梦想与她 提交于 2019-12-12 19:17:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!