#300 incomplete
Rob Anderton

Unsigned integers for MySQL

Reported by Rob Anderton | June 2nd, 2008 @ 03:20 PM

This was originally discussed but not resolved by others on the old Rails trac:

http://dev.rubyonrails.org/ticke...

I've attached a patch (with tests) that does the following:

  • changes the primary key data type for the MySQL adapter to be "int(11) unsigned"
  • changes the references (aka belongs_to) 'sexy' migration methods to generate unsigned foreign key fields by default when using MySQL (other adapters remain unchanged)
  • adds an :unsigned option (defaults to false) to table creation and modification migrations, so for example you can now do things like this:
t.integer :category_id, :null => true, :unsigned => true
change_column :suppliers, :category_id, :integer, :unsigned => false
  • updates the SchemaDumper to understand unsigned integer fields

The unsigned option will be silently ignored by all but the MySQL adaptor. I've tested with MySQL 5.1 and SQLite 3.

Comments and changes to this ticket

  • Pratik

    Pratik June 5th, 2008 @ 10:34 PM

    • → State changed from “new” to “incomplete”
    • → Assigned user changed from “” to “Pratik”

    Not a good idea to create unsigned primary keys by default. However, Rails should allow people to do it nevertheless. I think the format could be something like :

    create_table(:categories_suppliers, :id => :unsigned) do |t|
    ....
    end
    
  • Rob Anderton

    Rob Anderton June 9th, 2008 @ 09:15 PM

    Updated patch attached: this doesn't touch the primary keys at all, it just adds an :unsigned option to migrations.

    I have some other, non-MySQL specific, ideas for primary key options, and will submit these in a separate patch (which will also include the ability for specifying an unsigned PK in MySQL) when I've had chance to work on them.

  • Jason L Perry

    Jason L Perry September 23rd, 2008 @ 03:58 PM

    • → Tag changed from “” to “activerecord migrations patch”

    @pratik: Why is it not a good idea to create unsigned primary keys by default? I've read over the mailing list thread related to this ticket (http://www.ruby-forum.com/topic/..., is your concern just regarding backward compatibility?

    That said, I'd really like to be able to use :unsigned => true in migrations, regardless of any changes to the primary keys.

  • Rob Anderton

    Rob Anderton September 30th, 2008 @ 11:12 AM

    My understanding from the discussions was that changing the default would break backwards compatibility and that even though, as I pointed out, changes to the behaviour of the :limit option on numeric columns did the same thing, it wasn't acceptable for this patch.

    I'm still intending to either release a more configurable patch (there's a bit more I'd like to do with primary keys) or a plugin. Hopefully I'll get chance over the next few weeks as it's been on my todo list for too long now :)

    In the meantime I've uploaded the monkey-patch edition, which I've been using successfully on recent projects, to my blog

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Source available from github

The Git repository resides at http://github.com/rails

Check out the current development trunk (Edge Rails) with:

git clone git://github.com/rails/rails.git

Creating or reviewing a patch

See the contributor guide.

Creating a feature request

Please don't. If you want a new feature in Rails, you'll have to pull up your sleeves and get busy yourself. Or convince someone else to do it. See the contributor guide on how to get going. But posting them here is just going to lead to ticket root.

Creating a bug report

When creating a bug report, be sure to include as much relevant information as possible. Post the code sample that causes the problem. Preferably, alter the unit tests and show through either changed or added tests how the expected behavior is not occuring.

Security vulnerabilities should be reported via an email to security@rubyonrails.org, do not use trac for reporting security vulnerabilities. All content in trac is publicly available as soon as it is posted.

Then don't get your hopes up. Unless you have a "Code Red, Mission Critical, The World is Coming to an End" kinda bug, you're creating this ticket in the hope that others with the same problem will be able to collaborate with you on solving it. Do not expect that the ticket automatically will see any activity or that others will jump to fix it. Creating a ticket like this is mostly to help yourself start on the path of fixing the problem and for others to sign on to with a "I'm having this problem too".

Shared Ticket Bins