Skip to content

Instantly share code, notes, and snippets.

@claptimes5
Created August 6, 2014 15:40
Show Gist options
  • Save claptimes5/51f567b2a4f0b5856f31 to your computer and use it in GitHub Desktop.
Save claptimes5/51f567b2a4f0b5856f31 to your computer and use it in GitHub Desktop.
ActiveRecord incorrect update query when using custom primary key
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rack', github: 'rack/rack'
gem 'rails', github: 'rails/rails'
gem 'arel', github: 'rails/arel'
gem 'sqlite3'
gem 'i18n', github: 'svenfuchs/i18n'
GEMFILE
system 'bundle'
end
require 'bundler'
Bundler.setup(:default)
require 'active_record'
require 'minitest/autorun'
require 'logger'
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :programs, id: false do |t|
t.integer :vendor_id
end
end
class Program < ActiveRecord::Base
self.primary_key = 'vendor_id'
end
class BugTest < Minitest::Test
def test_update_primary_key_value
program = Program.create!(vendor_id: 1)
program.vendor_id = 2
# This generates:
# UPDATE "programs" SET "vendor_id" = ? WHERE "programs"."vendor_id" = 2 [["vendor_id", 2]]
program.save!
program = Program.first
assert_equal 2, program.vendor_id
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment