如何使用Rails 2.1和MySQL定义BigInt主键?

时间:2022-07-19 16:52:35

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.

从Rails 2.1开始,如果在迁移中定义一个新列,其类型设置为:integer,并且:limit设置为5或更多,则实际在MySQL数据库中创建的列将是BigInt类型。那很完美。

But I cannot figure out how to create a table with a BigInt primary key.

但我无法弄清楚如何使用BigInt主键创建表。

Any clues?

2 个解决方案

#1


1  

I just stumbled upon this plugin: it seems to answer this very question.

我只是偶然发现了这个插件:它似乎回答了这个问题。

#2


1  

This works in rails 3 not sure if it would work in rails 2.

这在导轨3中起作用,不确定它是否可以在导轨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

在我的应用程序中,我需要我的主键是bigint unsigned。我最终做的是放入我的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

这让rails自动创建id作为BigInts。当我从另一张桌子做一个参考时,我会做以下事情

t.column :product_id, 'BIGINT UNSIGNED'

#1


1  

I just stumbled upon this plugin: it seems to answer this very question.

我只是偶然发现了这个插件:它似乎回答了这个问题。

#2


1  

This works in rails 3 not sure if it would work in rails 2.

这在导轨3中起作用,不确定它是否可以在导轨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

在我的应用程序中,我需要我的主键是bigint unsigned。我最终做的是放入我的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

这让rails自动创建id作为BigInts。当我从另一张桌子做一个参考时,我会做以下事情

t.column :product_id, 'BIGINT UNSIGNED'