使用rake db:migrate命令和mysql在Rails中创建表的PRIMARY KEY问题

时间:2015-11-16 19:22:16

标签: mysql ruby-on-rails rake primary-key dbmigrate

我的rails版本是4.0.0,我的mysql版本是Ver 14.14 Distrib 5.7.9,适用于Win64(x86_64)。我正在使用旧版本的rails,因为我根据上一个问题Here得到了与mysql的一些冲突。 (查看Kalelc已批准的答案以获取我的追索权)

运行时

rake db:migrate 

我收到以下错误

==  CreateUsers: migrating ====================================================
-- create_table(:users)
rake aborted!
StandardError: An error has occurred, all later migrations canceled:

Mysql2::Error: All parts of a PRIMARY KEY must be NOT NULL; if you need NULL in a key, use UNIQUE instead: CREATE TABLE `users` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY, `first_name` varchar(25), `last_name` varchar(50), `email` varchar(255) DEFAULT '' NOT NULL, `password` varchar(40), `created_at` datetime, `updated_at` datetime) ENGINE=InnoDBC:/Users/Lizanne/Documents/Code/Sites/simple_cms/db/migrate/20151116154434_create_users.rb:3:in `up'
C:in `migrate'
ActiveRecord::StatementInvalid: Mysql2::Error: All parts of a PRIMARY KEY must be NOT NULL; if you need NULL in a key, use UNIQUE instead: CREATE TABLE `users` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY, `first_name` varchar(25), `last_name` varchar(50), `email` varchar(255) DEFAULT '' NOT NULL, `password` varchar(40), `created_at` datetime, `updated_at` datetime) ENGINE=InnoDB
C:/Users/Lizanne/Documents/Code/Sites/simple_cms/db/migrate/20151116154434_create_users.rb:3:in `up'
C:in `migrate'
Mysql2::Error: All parts of a PRIMARY KEY must be NOT NULL; if you need NULL in a key, use UNIQUE instead
C:/Users/Lizanne/Documents/Code/Sites/simple_cms/db/migrate/20151116154434_create_users.rb:3:in `up'
C:in `migrate'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

我的代码中没有将任何值设置为NULL,这是代码

Class CreateUsers < ActiveRecord::Migration

  def up
    create_table :users do |t| 
      t.column "first_name", :string, :limit => 25 
      t.string "last_name", :limit => 50
      t.string "email", :default => "", :null => false 
      t.string "password", :limit => 40
      t.timestamps
    end
  end

  def down
    drop_table :users
  end
end

此代码完全,如我所遵循的教程中所示。我还调查了堆栈溢出的其他similar issues here并遵循了给出的建议。我试过了monkey patch as suggested

# lib/patches/abastract_mysql_adapter.rb
class ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter
  NATIVE_DATABASE_TYPES[:primary_key] = "int(11) auto_increment PRIMARY KEY"
end

我将这个文件插入到我在simple_cms应用程序的lib中创建的名为patches的文件夹中。我已将文件保存为&#34; abstract_mysql_adapter.rb&#34; 正如同一个猴子补丁中所建议的那样 我已使用以下

更新了simple_cms应用程序的environment.rb
require File.expand_path('../../lib/patches/abstract_mysql_adapter.rb', __FILE__)

如果我然后运行rake db:migrate命令

rake aborted!
LoadError: cannot load such file -- C:/Users/Lizanne/Documents/Code/Sites/simple_cms/lib/patches/abstract_mysql_adapter.rb
C:/Users/Lizanne/Documents/Code/Sites/simple_cms/config/environment.rb:3:in `<top (required)>'
Tasks: TOP => db:migrate => environment
(See full trace by running task with --trace)

C:/Users/Lizanne/Documents/Code/Sites/simple_cms/lib/patches/abstract_mysql_adapter.rb绝对是猴子补丁的路径。我把补丁放在了错误的地方吗?我在这里做错了什么,在这个问题上摸不着头脑?如果对某些人来说这是显而易见的,我会道歉,但是在经过很长时间的禁欲后我又回到了编码状态,我无法理解这个问题。非常感谢您的帮助:)

4 个答案:

答案 0 :(得分:71)

我最近也面临同样的问题。

MySQL 5.7不再支持主键的空默认值。

通过覆盖MySql中主键的Native默认值,您可以解决问题。

在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

答案 1 :(得分:11)

上面的解决方案没有运气(我的环境:Rails 3.0.20,MySQL 5.7.13,Ruby 1.9.3p551)。能够通过覆盖ActiveRecord::ConnectionAdapters::ColumnDefinition类来绕过它。见下文:

class ActiveRecord::ConnectionAdapters::ColumnDefinition
  def sql_type
    type.to_sym == :primary_key ? 'int(11) auto_increment PRIMARY KEY' : base.type_to_sql(type.to_sym, limit, precision, scale) rescue type
  end 
end

在config / initializers / column_definition.rb

中存储

答案 2 :(得分:3)

我也有这个问题(mysql 5.7.17和Rails 4.0.0)。 我通过添加file config/initializers/mysql2_adapter.rb

来修复它
require 'active_record/connection_adapters/mysql2_adapter'
class ActiveRecord::ConnectionAdapters::Mysql2Adapter
  NATIVE_DATABASE_TYPES[:primary_key] = "int(11) auto_increment PRIMARY KEY"
end

然后,在我的everionment.rb文件中:

require File.expand_path('../initializers/mysql2_adapter', __FILE__)

答案 3 :(得分:0)

我通过JRuby上的Rails应用程序解决了这个问题,我正在升级mysql-adapter

我使用的是gem activerecord -jdbcmysql-adapter v1.3.14并升级到v1.3.21

在猴子修补解决方案之前检查你的jdbc适配器版本。