Gem Load Error is: undefined method `type_cast_fro

2019-08-25 12:04发布

问题:

I'm struggling for many hours now using activerecord-jdbcmssql-adapter with Rails 5.1 (or 5.2). I've installed ant to be able to compile the gem during the bundling. The bundle process runs fine but when I want to start rails I'm just getting

Bundler::GemRequireError: There was an error while trying to load the gem 'activerecord-jdbcmssql-adapter'.
Gem Load Error is: undefined method `type_cast_from_database' for class `ArJdbc::MSSQL::UUIDType'

I've googled a lot but didn't find a solution or a hint what causes this error. Here is some more information:

Java version: Java(TM) SE Runtime Environment (build 1.8.0_171-b11)

Ruby: jruby-9.1.17.0

OS: Win10

Part of the Gemfile

ruby '2.3.3'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.1.6'

gem 'activerecord-jdbc-adapter', github: "jruby/activerecord-jdbc-adapter", :branch => "51-stable"
gem 'activerecord-jdbcmssql-adapter',github: "jruby/activerecord-jdbc-adapter",:branch => "51-stable"
gem 'activerecord-oracle_enhanced-adapter','~> 1.8.0'

The Backtrace

Q:\Ruby\jruby-9.1.17.0\bin\jruby.exe -S rails s
Bundler::GemRequireError: There was an error while trying to load the gem 'activerecord-jdbcmssql-adapter'.
Gem Load Error is: undefined method `type_cast_from_database' for class `ArJdbc::MSSQL::UUIDType'
Backtrace for gem load error is:
org/jruby/RubyModule.java:2841:in `alias_method'
o:/bundler/bundler/gems/activerecord-jdbc-adapter-4eebab4df17d/lib/arjdbc/mssql/types.rb:319:in `<class:UUIDType>'
o:/bundler/bundler/gems/activerecord-jdbc-adapter-4eebab4df17d/lib/arjdbc/mssql/types.rb:316:in `<module:MSSQL>'
o:/bundler/bundler/gems/activerecord-jdbc-adapter-4eebab4df17d/lib/arjdbc/mssql/types.rb:2:in `<module:ArJdbc>'
o:/bundler/bundler/gems/activerecord-jdbc-adapter-4eebab4df17d/lib/arjdbc/mssql/types.rb:1:in `<main>'
org/jruby/RubyKernel.java:956:in `require'
o:/bundler/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `block in require'
o:/bundler/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:258:in `load_dependency'
o:/bundler/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `require'
o:/bundler/bundler/gems/activerecord-jdbc-adapter-4eebab4df17d/lib/arjdbc/mssql/adapter.rb:1:in `<module:(root)>'
o:/bundler/bundler/gems/activerecord-jdbc-adapter-4eebab4df17d/lib/arjdbc/mssql/adapter.rb:37:in `<module:MSSQL>'
o:/bundler/bundler/gems/activerecord-jdbc-adapter-4eebab4df17d/lib/arjdbc/mssql/adapter.rb:30:in `<main>'
org/jruby/RubyKernel.java:956:in `require'
o:/bundler/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `block in require'
o:/bundler/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:258:in `load_dependency'
o:/bundler/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `require'
o:/bundler/bundler/gems/activerecord-jdbc-adapter-4eebab4df17d/lib/arjdbc/mssql/adapter.rb:29:in `<main>'
org/jruby/RubyKernel.java:956:in `require'
o:/bundler/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `block in require'
o:/bundler/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:258:in `load_dependency'
o:/bundler/gems/activesupport-5.1.6/lib/active_support/dependencies.rb:292:in `require'
o:/bundler/bundler/gems/activerecord-jdbc-adapter-4eebab4df17d/lib/arjdbc/mssql.rb:1:in `<main>'
org/jruby/RubyKernel.java:956:in `require'
o:/bundler/bundler/gems/activerecord-jdbc-adapter-4eebab4df17d/lib/arjdbc/mssql.rb:2:in `block in (root)'
org/jruby/RubyArray.java:1735:in `each'
o:/bundler/bundler/gems/activerecord-jdbc-adapter-4eebab4df17d/activerecord-jdbcmssql-adapter/lib/activerecord-jdbcmssql-adapter.rb:1:in `block in (root)'
org/jruby/RubyArray.java:1735:in `each'
o:/bundler/bundler/gems/activerecord-jdbc-adapter-4eebab4df17d/activerecord-jdbcmssql-adapter/lib/activerecord-jdbcmssql-adapter.rb:2:in `(root)'
Q:/Ruby/jruby-9.1.17.0/lib/ruby/gems/shared/gems/bundler-1.16.2/lib/bundler/runtime.rb:1:in `(root)'
Q:/Ruby/jruby-9.1.17.0/lib/ruby/gems/shared/gems/bundler-1.16.2/lib/bundler/runtime.rb:81:in `<main>'
org/jruby/RubyKernel.java:956:in `require'
Q:/Ruby/jruby-9.1.17.0/lib/ruby/gems/shared/gems/bundler-1.16.2/lib/bundler/runtime.rb:76:in `block in require'
org/jruby/RubyKernel.java:1765:in `tap'
Q:/Ruby/jruby-9.1.17.0/lib/ruby/gems/shared/gems/bundler-1.16.2/lib/bundler/runtime.rb:65:in `require'
Q:/Ruby/jruby-9.1.17.0/lib/ruby/gems/shared/gems/bundler-1.16.2/lib/bundler.rb:114:in `require'
O:/git/xxxx/config/application.rb:7:in `(root)'
o:/bundler/gems/railties-5.1.6/lib/rails/commands/server/server_command.rb:1:in `(root)'
o:/bundler/gems/railties-5.1.6/lib/rails/commands/server/server_command.rb:133:in `perform'
o:/bundler/gems/railties-5.1.6/lib/rails/commands/server/server_command.rb:130:in `perform'
o:/bundler/gems/thor-0.20.0/lib/thor/command.rb:27:in `<main>'
org/jruby/RubyKernel.java:956:in `require'
bin/rails:4:in `<main>'
Bundler Error Backtrace:

  block in require at Q:/Ruby/jruby-9.1.17.0/lib/ruby/gems/shared/gems/bundler-1.16.2/lib/bundler/runtime.rb:84
              each at org/jruby/RubyArray.java:1735
  block in require at Q:/Ruby/jruby-9.1.17.0/lib/ruby/gems/shared/gems/bundler-1.16.2/lib/bundler/runtime.rb:76
              each at org/jruby/RubyArray.java:1735
          require at Q:/Ruby/jruby-9.1.17.0/lib/ruby/gems/shared/gems/bundler-1.16.2/lib/bundler/runtime.rb:65
          require at Q:/Ruby/jruby-9.1.17.0/lib/ruby/gems/shared/gems/bundler-1.16.2/lib/bundler.rb:114
            <main> at O:/git/xxx/config/application.rb:7
          require at org/jruby/RubyKernel.java:956
  block in (root) at o:/bundler/gems/railties-5.1.6/lib/rails/commands/server/server_command.rb:1
              tap at org/jruby/RubyKernel.java:1765
          perform at o:/bundler/gems/railties-5.1.6/lib/rails/commands/server/server_command.rb:133
          perform at o:/bundler/gems/railties-5.1.6/lib/rails/commands/server/server_command.rb:130
              run at o:/bundler/gems/thor-0.20.0/lib/thor/command.rb:27
    invoke_command at o:/bundler/gems/thor-0.20.0/lib/thor/invocation.rb:126
          dispatch at o:/bundler/gems/thor-0.20.0/lib/thor.rb:387
          perform at o:/bundler/gems/railties-5.1.6/lib/rails/command/base.rb:63
            <main> at o:/bundler/gems/railties-5.1.6/lib/rails/command.rb:44
          require at org/jruby/R

回答1:

AR-JDBC does not support MS-SQL for AR >= 5.0 there simply wasn't enough resources to implement the SQLServer adapter.

however, the sqlserver adapter seems to have JRuby support these days. there's some minor details where it behaves differently, but not much to worry about (except that it might be noticeably slower due pure .rb parts).



回答2:

I tried it with older rails version: 4.2.10. Here is my gemfile below.
After that i can run rails s, rails c and connect to my mssql db.
Note this is just the from the rails new app_name command.

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }



# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 4.2.10'
# Use jdbcsqlite3 as the database for Active Record
gem 'activerecord-jdbcsqlite3-adapter', '~> 1.3', '>= 1.3.20'
gem 'activerecord-jdbcmssql-adapter', '~> 1.3', '>= 1.3.20'
# Use Puma as the app server
gem 'puma', '~> 3.11'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
gem 'therubyrhino'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use ActiveStorage variant
# gem 'mini_magick', '~> 4.8'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

# Reduces boot times through caching; required in config/boot.rb
# gem 'bootsnap', '>= 1.1.0', require: false


# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

Here is my config/database.yml:

default: &default
  adapter: jdbcmssql
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  timeout: 5000
  mode: dblib
  host: <%= ENV.fetch("MSSQL_HOST") { 'localhost' } %>
  port: <%= ENV.fetch("MSSQL_PORT") { 1433 } %>
  username: SA
  password: '!P4ssw0rd'

development:
  <<: *default
  database: development_mssql  

Here is output when i run rbenv local:

$ rbenv local
jruby-9.2.0.0