In Rails / ActiveRecord 3, how do I change the default primary key type for MySQL?

In Rails 3, how do you change the default primary key type, e.g. BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY

In my case, I'm interested in MySQL.

For Rails 2, you can see the answer to the question "How to use long identifiers in Rails?" 1

In Rails 3, however, this will throw an error. I'm not sure if this is because the class has not been used for a long time, or if the code should go in one place. You can see in active_record/connection_adapters/mysql_adapter.rb that the constant NATIVE_DATABASE_TYPES is still defined.

What is the correct way to achieve the same effect in Rails 3?

+4
source share
2 answers

The problem is that connection adapters now load lazily after starting all initializers.

Try explicitly requesting the code module that you use to render monkeys harmless:

 require 'active_record/connection_adapters/mysql_adapter' ActiveRecord::ConnectionAdapters::MysqlAdapter::NATIVE_DATABASE_TYPES[:primary_key] = "BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY".freeze 

I do not know if adding this parameter to config/environment.rb works. In any case, I added it to config/application.rb because it seemed more appropriate.

+1
source

if you are using the mysql2 adapter, change the above code to:

 require 'active_record/connection_adapters/mysql2_adapter' ActiveRecord::ConnectionAdapters::Mysql2Adapter::NATIVE_DATABASE_TYPES[:primary_key] = .... 
0
source

Source: https://habr.com/ru/post/1313574/


All Articles