在Rails中创建主键的表和问题

时间:2014-01-12 14:01:00

标签: mysql ruby-on-rails primary-key mysql2

当我尝试使用Mysql2作为数据库管理器在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

3 个答案:

答案 0 :(得分:84)

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

  

使用Rails 2.3.5,MySQL版本5.7.9和mysql gem,你需要拥有   这个位作为初始化器   配置/初始化/ 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

答案 1 :(得分:29)

从mysql 5.7破解更改页面:

  

PRIMARY KEY中的列必须为NOT NULL,但如果显式声明   因为NULL没有产生错误。现在发生错误。例如,a   诸如CREATE TABLE t(i INT NULL PRIMARY KEY)之类的语句被拒绝。   类似的ALTER TABLE语句也是如此。 (Bug#13995622,   Bug#66987,Bug#15967545,Bug#16545198)

Changes in MySQL 5.7.3 (2013-12-03, Milestone 13)

此问题was fixed 3个月前。我不知道它将成为

的一部分的Rails版本

猴子补丁是here

修改 这个修复程序合并到master中已经有一年多了。因此,它必须是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中。如果升级到至少v4.1.0是一个选项,那么它应该解决问题。

答案 2 :(得分:2)

我遇到此问题使用mysql 5.7.x运行rails 4.0.x应用程序。我能够通过升级到rails 4.2.x并升级我的宝石来修复它。

(我确信这些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