Should a new foreign_key type be added to adapters so that add_reference/add_belongs_to could use that vs. hardcoding the integer type?

Although there is the undocumented native_database_types method that returns a hash in each ActiveRecord adapter included in Rails, e.g.:

def native_database_types #:nodoc:

NATIVE_DATABASE_TYPES

end

If you use the add_reference/add_belongs_to methods in a migration, they use the integer type rather than the primary_key type for foreign_keys, which is correct since the primary key type can define autoincrement, however you want the foreign key column type itself (e.g. integer, etc.) to match the primary key’s type, so it seems like there should be another foreign_key type defined in each adapter. This way you could actually get the foreign_key type from the adapter in the add_reference/add_belongs_to methods and wouldn’t need to instead use add_column as a workaround.

Although there is the undocumented native_database_types method that returns a hash in each ActiveRecord adapter included in Rails.

It is not undocumented, it is private API and should not be used in applications.

If you use the add_reference/add_belongs_to methods in a migration, they use the integer type rather than the primary_key type for foreign_keys, which is correct since the primary key type can define autoincrement, however you want the foreign key column type itself (e.g. integer, etc.) to match the primary key’s type, so it seems like there should be another foreign_key type defined in each adapter. This way you could actually get the foreign_key type from the adapter in the add_reference/add_belongs_to methods and wouldn’t need to instead use add_column as a workaround.

Instead of adding a new type why not make add_reference/add_belongs_to accept the column type? This will provide a way of declaring the foreign key column with the right type without having to add a type that is not native.

Rafael Mendonça França
http://twitter.com/rafaelfranca
https://github.com/rafaelfranca

Specifying the :type on references has already been added. See https://github.com/rails/rails/pull/16231 for more details.