在Rails中用主键创build表和问题

当我尝试在使用Mysql2作为数据库pipe理器的Rails中运行以下代码时:

rake db:migrate 

我得到以下错误:

  rake aborted! "Mysql2::Error: All parts of a PRIMARY KEY must be NOT NULL:" 

为什么我得到这个错误,如果表中的主键默认不是“null”?

迁移代码,但是:

 class CreateUsers < ActiveRecord::Migration def change create_table :users do |t| t.string "first_name" t.timestamps end end end 

我之前也有同样的问题,我按照这里解决https://github.com/rails/rails/pull/13247#issuecomment-32425844

使用Rails 2.3.5,MySQL版本5.7.9和MySQL gem,您需要在config / initializers / abstract_mysql_adapter.rb中将此位设置为初始值设定项:

 class ActiveRecord::ConnectionAdapters::MysqlAdapter NATIVE_DATABASE_TYPES[:primary_key] = "int(11) auto_increment PRIMARY KEY" end 

对于mysql2,它应该是config / initializers / abstract_mysql2_adapter.rb:

 class ActiveRecord::ConnectionAdapters::Mysql2Adapter NATIVE_DATABASE_TYPES[:primary_key] = "int(11) auto_increment PRIMARY KEY" end 

从MySQL 5.7中断更改页面:

PRIMARY KEY中的列必须不为NULL,但是如果显式声明为NULL,则不会产生错误。 现在发生错误。 例如,诸如CREATE TABLE t(i INT NULL PRIMARY KEY)的语句被拒绝。 类似的ALTER TABLE语句也会发生同样的情况。 (Bug#13995622,Bug#66987,Bug#15967545,Bug#16545198)

MySQL 5.7.3(2013-12-03,里程碑13)

这个问题在 3个月前解决了。 我不知道它将成为Rails版本的一部分

猴子补丁在这里

编辑:已经有一年多的时间了,这个修补程序被合并到了主文件中。 所以,它必须是Rails最后一个版本的一部分。

编辑:确实,修复这个提交可以在: https : //github.com/yahonda/rails/commit/b6655885ef13cf8d1705dc9b5232846f0207febd ,并显示该修补程序包含在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 。 如果升级到至lessv4.1.0是一个选项,那么它应该解决这个问题。

我有这个问题使用mysql 5.7.x运行一个rails 4.0.x应用程序。 我能够通过升级到轨道4.2.x和升级我的gem来解决它。

(我确定这些gemfiles需要工作,但希望他们仍然有帮助)

旧的Gemfile

 source 'https://rubygems.org' ruby '2.0.0' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '4.0.13' gem 'mysql2' # Use unicorn as the app server # gem 'unicorn' gem 'sidekiq' gem 'sinatra' gem 'whenever' gem 'thin' gem 'faye' gem 'faye-websocket' #pdf generation gem 'wicked_pdf' gem 'wkhtmltopdf-binary' # Assets gem 'backbone-on-rails', '~> 0.9.9' gem 'coffee-rails', '~> 4.0.1' gem 'coffee-script-source', '=1.8.0' gem 'ejs' # uses templates for backbone gem 'jquery-rails' gem 'jquery-ui-rails' gem 'neat' gem 'sass-rails', '~> 4.0.3' gem 'therubyracer' gem 'turbolinks' gem 'uglifier', '>= 1.3.0' gem 'active_model_serializers', '0.8.3' gem 'acts_as_list' gem 'bcrypt-ruby', '~> 3.1.2' gem 'bootstrap-sass', '~> 3.2.0.0' gem 'bower-rails' gem 'carrierwave' gem 'enumerize' gem 'embedly' # Url to Attachment Processing gem 'fancybox2-rails', '~> 0.2.4' gem 'flamegraph' gem 'fog' # s3 storage gem 'globalize' # I18n for tagging, etc. gem 'globalize-accessors' gem 'i18n-tasks', '~> 0.8.3' gem 'hirb' gem 'kaminari' gem 'nokogiri' gem 'newrelic_rpm' gem 'select2-rails' # Token Fields gem 'twilio-ruby' gem 'rails_autolink' # Convert urls to links gem 'redis', '3.2.1' gem 'rest-client' gem 'rmagick' gem 'rubyzip', :require => 'zip' # Zip files together gem 'unf' gem 'mailgun-ruby', '~>1.0.3', require: 'mailgun' # Deployments gem 'mina' group :doc do gem 'sdoc' end group :test do gem 'rspec-rails', "= 2.14.2" gem 'shoulda-matchers', :require => false gem 'simplecov', '~> 0.9.2' gem 'database_cleaner' gem 'codeclimate-test-reporter', require: nil end group :development, :test do gem 'quiet_assets' gem 'factory_girl_rails', "~> 4.0" gem 'guard', '>=2.1.0' gem 'guard-rspec', '= 4.2.9' gem 'spring', '1.3.6' gem 'spring-commands-rspec' gem 'foreman' end 

新的Gemfile

 source 'https://rubygems.org' ruby '2.3.0' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '4.2.5' gem 'mysql2' gem 'stackprof' # Use unicorn as the app server # gem 'unicorn' gem 'sidekiq' gem 'sinatra' gem 'whenever' gem 'thin' gem 'faye' gem 'faye-websocket' #pdf generation gem 'wicked_pdf' gem 'wkhtmltopdf-binary' # Assets gem 'backbone-on-rails', '~> 0.9.9' gem 'coffee-rails', '~> 4.0.1' gem 'coffee-script-source', '=1.8.0' gem 'ejs' # uses templates for backbone gem 'jquery-rails' gem 'jquery-ui-rails' gem 'neat' gem 'sass-rails', '~> 4.0.3' gem 'therubyracer' gem 'turbolinks' gem 'uglifier', '>= 1.3.0' gem 'active_model_serializers', '0.8.3' gem 'acts_as_list' gem 'bcrypt', '~> 3.1.2' gem 'bootstrap-sass', '~> 3.2.0.0' gem 'bower-rails' gem 'carrierwave' gem 'enumerize' gem 'embedly' # Url to Attachment Processing gem 'fancybox2-rails', '~> 0.2.4' gem 'flamegraph' gem 'fog' # s3 storage gem 'globalize' # I18n for tagging, etc. gem 'globalize-accessors' gem 'i18n-tasks', '~> 0.8.3' gem 'hirb' gem 'kaminari' gem 'nokogiri' gem 'newrelic_rpm' gem 'select2-rails' # Token Fields gem 'twilio-ruby' gem 'rails_autolink' # Convert urls to links gem 'redis', '3.2.1' gem 'rest-client' gem 'rmagick' gem 'rubyzip', :require => 'zip' # Zip files together gem 'unf' gem 'mailgun-ruby', '~>1.0.3', require: 'mailgun' gem 'rack-cors', :require => 'rack/cors' # Deployments gem 'mina' group :doc do gem 'sdoc' end group :test do gem 'rspec-rails', "= 2.14.2" gem 'shoulda-matchers', :require => false gem 'simplecov', '~> 0.9.2' gem 'database_cleaner' gem 'codeclimate-test-reporter', require: nil end group :development, :test do gem 'quiet_assets' gem 'factory_girl_rails', "~> 4.0" gem 'guard', '>=2.1.0' gem 'guard-rspec', '= 4.2.9' gem 'spring', '1.6.1' gem 'spring-commands-rspec' gem 'foreman' end