Permalink
Browse files

Create a new test case wherein we have a table with no auto-increment…

…ed primary key. Fix bug in adapter code that looks up PK based on the SQL when it should be passing the table name
  • Loading branch information...
1 parent 8c7e5e8 commit f950d17fd9c314a4f13bc12315cf55e8cd19a583 Rob Goretsky committed Dec 18, 2013
Showing with 36 additions and 1 deletion.
  1. +1 −1 lib/arjdbc/teradata/adapter.rb
  2. +18 −0 spec/models/no_autoincrement_pk.rb
  3. +17 −0 spec/no_autoincrement_pk_spec.rb
@@ -137,7 +137,7 @@ def _execute(sql, name = nil)
end if self.class.lowercase_schema_reflection
result
elsif self.class.insert?(sql)
- (@connection.execute_insert(sql) or last_insert_id(sql)).to_i
+ (@connection.execute_insert(sql) or last_insert_id(_table_name_from_insert(sql))).to_i
else
@connection.execute_update(sql)
end
@@ -0,0 +1,18 @@
+class CreateNoAutoincrementPks < ActiveRecord::Migration
+ def self.up
+ create_table :no_autoincrement_pks, :id=>false do |t|
+ t.integer :post_id
+ t.string :title
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :no_autoincrement_pks
+ end
+
+end
+
+class NoAutoincrementPk < ActiveRecord::Base
+ self.primary_key = 'post_id'
+end
@@ -0,0 +1,17 @@
+require 'spec_helper'
+require 'models/no_autoincrement_pk'
+
+describe 'NoAutoincrementPkSpec' do
+ it 'should be able to create a record and manually set the post_id' do
+ CreateNoAutoincrementPks.up
+ obj = NoAutoincrementPk.new
+ obj.post_id = 12345
+ obj.title = 'Sample title with 3 or more periods ...'
+ obj.save.should be_true
+ obj.reload
+ obj.id.should eq(12345)
+ found = NoAutoincrementPk.find(12345)
+ found.id.should eq(12345)
+ CreateNoAutoincrementPks.down
+ end
+end

0 comments on commit f950d17

Please sign in to comment.