Bugs item #11567, was opened at 2007-06-14 02:01
You can respond by visiting:
http://rubyforge.org/tracker/?func=detail&atid=7857&aid=11567&group_id=2014
Category: AR-JDBC
Group: None
>Status: Closed
Resolution: None
Priority: 3
Submitted By: Alexander Kireyev (b_krolik)
Assigned to: Nobody (None)
Summary: ActiveRecord-JDBC does not work with many-to-many relationships with
HSQLDB
Initial Comment:
ActiveRecord-JDBC 0.4, HSQLDB 1.8.0.7
In my project I have 2 entities with many-to-many relationship
class Book < ActiveRecord::Base
has_and_belongs_to_many :labels
end
class Label < ActiveRecord::Base
has_and_belongs_to_many :books
end
and a many-to-many relationship through the following table
create_table :books_labels do |t|
t.column :book_id, :int
t.column :label_id, :int
end
when i try to add a label to a book the exception occurs
>> Book.create
>> => #<Book:0x1c1c9dc @errors=#<ActiveRecord::Errors:0x182ab3e @errors={},
>> @base=#<Book:0x1c1c9dc ...>>, @new_record_before_save=true,
>> @attributes={"author"=>nil, "title"=>nil, "id"=>6}, @new_record=false>
Book.find(:all)
=> [#<Book:0x126f29f @attributes={"author"=>nil, "title"=>nil, "id"=>"5"}>,
#<Book:0x16f13bb @attributes={"author"=>nil, "title"=>nil, "id"=>"6"}>]
>> Label.create
>> => #<Label:0x1d6e4a4 @errors=#<ActiveRecord::Errors:0x13ceab3 @errors={},
>> @base=#<Label:0x1d6e4a4 ...>>, @new_record_before_save=true,
>> @attributes={"name"=>nil, "id"=>3}, @new_record=false>
a = Book.find(6)
>> => #<Book:0x1ec1774 @attributes={"author"=>nil, "title"=>nil, "id"=>"6"}>
b = Label.find(3)
>> => #<Label:0xc58432 @attributes={"name"=>nil, "id"=>"3"}>
a.labels = [b]
ActiveRecord::StatementInvalid: NativeException: java.sql.SQLException: This
function is not supported: INSERT INTO books_labels (id, label_id, book_id)
VALUES (3, 3, 6)
from
/home/alice/jruby-1.0/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/transactions.rb:101:in
`transaction'
from
/home/alice/jruby-1.0/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/transactions.rb:121:in
`transaction'
from
/home/alice/jruby-1.0/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/associations/association_collection.rb:146:in
`replace'
from
/home/alice/jruby-1.0/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/associations.rb:950:in
`labels='
from
/home/alice/jruby-1.0/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/associations.rb:852:in
`labels='
from (irb):6:in `binding'
from /home/alice/jruby-1.0/lib/ruby/1.8/irb.rb:150:in `eval_input'
from /home/alice/jruby-1.0/lib/ruby/1.8/irb.rb:70:in `signal_status'
from /home/alice/jruby-1.0/lib/ruby/1.8/irb.rb:189:in `eval_input'
from /home/alice/jruby-1.0/lib/ruby/1.8/irb.rb:70:in
`each_top_level_statement'
from /home/alice/jruby-1.0>> /lib/ruby/1.8/irb.rb:190:in `loop'
from /home/alice/jruby-1.0/lib/ruby/1.8/irb.rb:190:in `catch'
from /home/alice/jruby-1.0/lib/ruby/1.8/irb.rb:190:in `eval_input'
from /home/alice/jruby-1.0/lib/ruby/1.8/irb.rb:70:in `start'
from :-1:in `catch'
from /home/alice/jruby-1.0/lib/ruby/1.8/irb.rb:71:in `start'
from :-1
The problem is that ActiveRecord-JDBC tries to call a method
Statement.executeUpdate(String, int) which is not supported by HSQLDB. Since
README file for AR-JDBC claims that HSQLDB is fully supported I decided to file
this as a bug.
----------------------------------------------------------------------
Comment By: Matt Williams (ottercat)
Date: 2007-07-07 13:56
Message:
Here's a patch which fixes it:
Index: jdbc_hsqldb.rb
===================================================================
--- jdbc_hsqldb.rb (revision 665)
+++ jdbc_hsqldb.rb (working copy)
@@ -121,6 +121,18 @@
Integer(select_value("SELECT IDENTITY() FROM #{table}"))
end
+ def _execute(sql, name = nil)
+ case sql.strip
+ when /\Ainsert/i:
+ insert(sql,name)
+ when /\A\(?\s*(select|show)/i:
+ @connection.execute_query(sql)
+ else
+ @connection.execute_update(sql)
+ end
+ end
+
+
def add_limit_offset!(sql, options) #:nodoc:
offset = options[:offset] || 0
bef = sql[7..-1]
----------------------------------------------------------------------
You can respond by visiting:
http://rubyforge.org/tracker/?func=detail&atid=7857&aid=11567&group_id=2014
_______________________________________________
Jruby-extras-devel mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/jruby-extras-devel