makandra / active_type

Make any Ruby object quack like ActiveRecord
MIT License
1.09k stars 74 forks source link

Can't run specs for Gemfile.3.2.mysql2: CREATE TABLE invalid statement #55

Closed mattbrictson closed 8 years ago

mattbrictson commented 8 years ago

I am trying to run the specs as instructed in the README using rake all:spec.

All specs for all gemfiles pass, with the exception of Gemfile.3.2.mysql2. I get an ActiveRecord::StatementInvalid error when spec/support/database.rb is trying to create the tables in the database:

All parts of a PRIMARY KEY must be NOT NULL; if you need NULL in a key, use UNIQUE instead:
CREATE TABLE `records` (
  `id` int(11) DEFAULT NULL auto_increment PRIMARY KEY, 
  `persisted_string` varchar(255), 
  `persisted_integer` int(11), 
  `persisted_time` datetime,  
  `persisted_date` date, 
  `persisted_boolean` tinyint(1)
) ENGINE=InnoDB

Again, this works fine for sqlite, pg, and for Gemfile.4.2.1.mysql2. It is only Gemfile.3.2.mysql2 that has the problem. Any ideas?

Full backtrace:

rake all:spec

gemfiles/Gemfile.3.2.mysql2

-- create_table(:records)
/Users/mbrictson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-3.2.22/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `query': Mysql2::Error: All parts of a PRIMARY KEY must be NOT NULL; if you need NULL in a key, use UNIQUE instead: CREATE TABLE `records` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY, `persisted_string` varchar(255), `persisted_integer` int(11), `persisted_time` datetime, `persisted_date` date, `persisted_boolean` tinyint(1)) ENGINE=InnoDB (ActiveRecord::StatementInvalid)
    from /Users/mbrictson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-3.2.22/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `block in execute'
    from /Users/mbrictson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-3.2.22/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
    from /Users/mbrictson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-3.2.22/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
    from /Users/mbrictson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-3.2.22/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
    from /Users/mbrictson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-3.2.22/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `execute'
    from /Users/mbrictson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-3.2.22/lib/active_record/connection_adapters/mysql2_adapter.rb:213:in `execute'
    from /Users/mbrictson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-3.2.22/lib/active_record/connection_adapters/abstract/schema_statements.rb:170:in `create_table'
    from /Users/mbrictson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-3.2.22/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:434:in `create_table'
    from /Users/mbrictson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-3.2.22/lib/active_record/migration.rb:466:in `block in method_missing'
    from /Users/mbrictson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-3.2.22/lib/active_record/migration.rb:438:in `block in say_with_time'
    from /Users/mbrictson/.rbenv/versions/2.2.3/lib/ruby/2.2.0/benchmark.rb:288:in `measure'
    from /Users/mbrictson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-3.2.22/lib/active_record/migration.rb:438:in `say_with_time'
    from /Users/mbrictson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-3.2.22/lib/active_record/migration.rb:458:in `method_missing'
    from /Users/mbrictson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activerecord-3.2.22/lib/active_record/migration.rb:334:in `method_missing'
    from /Users/mbrictson/Code/active_type/spec/support/database.rb:34:in `block in <top (required)>'
    from /Users/mbrictson/Code/active_type/spec/support/database.rb:32:in `class_eval'
    from /Users/mbrictson/Code/active_type/spec/support/database.rb:32:in `<top (required)>'
    from /Users/mbrictson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:251:in `require'
    from /Users/mbrictson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:251:in `block in require'
    from /Users/mbrictson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:236:in `load_dependency'
    from /Users/mbrictson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/activesupport-3.2.22/lib/active_support/dependencies.rb:251:in `require'
    from /Users/mbrictson/Code/active_type/spec/spec_helper.rb:9:in `block in <top (required)>'
    from /Users/mbrictson/Code/active_type/spec/spec_helper.rb:9:in `each'
    from /Users/mbrictson/Code/active_type/spec/spec_helper.rb:9:in `<top (required)>'
    from /Users/mbrictson/Code/active_type/spec/active_type/extended_record/single_table_inheritance_spec.rb:1:in `require'
    from /Users/mbrictson/Code/active_type/spec/active_type/extended_record/single_table_inheritance_spec.rb:1:in `<top (required)>'
    from /Users/mbrictson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `load'
    from /Users/mbrictson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `block in load_spec_files'
    from /Users/mbrictson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `each'
    from /Users/mbrictson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `load_spec_files'
    from /Users/mbrictson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rspec-core-2.14.8/lib/rspec/core/command_line.rb:22:in `run'
    from /Users/mbrictson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rspec-core-2.14.8/lib/rspec/core/runner.rb:80:in `run'
    from /Users/mbrictson/.rbenv/versions/2.2.3/lib/ruby/gems/2.2.0/gems/rspec-core-2.14.8/lib/rspec/core/runner.rb:17:in `block in autorun'
triskweline commented 8 years ago

Specs are green on Mysql 5.6.

I know Rails 3.2 has an issue with Mysql 5.7+ and you might be seeing that: https://github.com/rails/rails/pull/13247

There's a monkey patch in that link that might help.

mattbrictson commented 8 years ago

Thanks, I'll look into that. I never use Mysql for actual projects, so I'm not familiar with the ins and outs of the various versions.

mattbrictson commented 8 years ago

OK, I added this quick-and-dirty patch to active_type.rb in my working copy to allow it to work with Mysql 5.7+.

if ActiveRecord::VERSION::STRING == '3.2.22'
  begin
    require "mysql2"
    require "active_record/connection_adapters/abstract_mysql_adapter"
    class ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter
      NATIVE_DATABASE_TYPES[:primary_key] = "int(11) auto_increment PRIMARY KEY"
    end
  rescue LoadError
  end
end