When I try to run the following code in Rails using Mysql2 as database manager:
rake db:migrate
I obtain the following error:
rake aborted!
"Mysql2::Error: All parts of a PRIMARY KEY must be NOT NULL:"
Why do I get this error, if the primary key in a table by default is NOT "null"?
Migration code, however :
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string "first_name"
t.timestamps
end
end
end
From the mysql 5.7 breaking change page:
Changes in MySQL 5.7.3 (2013-12-03, Milestone 13)
This issue was fixed 3 months ago. I don't know what Rails version it's going to be part of
monkey patch is here
EDIT: It's been more than a year that this fix was merged into master. So, it must be part of the last version of Rails.
EDIT: Indeed, the commit that fixes this can be found at: https://github.com/yahonda/rails/commit/b6655885ef13cf8d1705dc9b5232846f0207febd , and shows that the fix is included in
v4.2.0.beta1, v4.1.6.rc1, v4.1.5, v4.1.4, v4.1.3, v4.1.2, v4.1.2.rc3, v4.1.2.rc2, v4.1.2.rc1, v4.1.1, v4.1.0, v4.1.0.rc2, v4.1.0.rc1, v4.1.0.beta2, v4.1.0.beta1
. If upgrading to at least v4.1.0 is an option for you, then it should fix the problem.I had a same problem before, and I solved according to here https://github.com/rails/rails/pull/13247#issuecomment-32425844
I had this issue running a rails 4.0.x app using mysql 5.7.x. I was able to fix it by upgrading to rails 4.2.x and upgrading my gems.
(I'm sure these gemfiles need work, but hopefully they are still helpful)
Old Gemfile
New Gemfile