waka / activerecord-mysql-unsigned

Enable to use the "unsigned" option in the integer type for migrating of ActiveRecord
MIT License
31 stars 12 forks source link

Default int display width difference #21

Open CHeavyarms opened 9 years ago

CHeavyarms commented 9 years ago

As far as I can tell, all ActiveRecord int columns are set with display width of 11.

Example below modified from Rails Guides:

CREATE TABLE products (
   id int(11) NOT NULL auto_increment,
   val int(11) NOT NULL,
   name varchar(255),
   PRIMARY KEY (id)
);

This makes sense with the MySQL default for signed int, since it requires 11 characters to display the min value and the negative sign. See this SO question.

Currently, the gem modifies the default behavior and all int value columns are created with a display width of 10. This causes slight differences in the structure.sql files created by ActiveRecord. In the spec migrations you refer to the int(10) being the default ActiveRecord primary_key, but that does not appear to be true.

Does it make more sense to respect the default ActiveRecord behavior and for signed int columns set the display width to 11 and for unsigned int columns set it to 10?

CREATE TABLE products (
   id int(10) unsigned NOT NULL auto_increment,
   val int(11) NOT NULL,
   n int(10) unsigned NOT NULL,
   name varchar(255),
   PRIMARY KEY (id)
);
CHeavyarms commented 9 years ago

After making sure I updated the gem to the latest, the behavior is correct. However, perhaps the spec migration should be updated?