Skip to content

Commit 6474765

Browse files
committed
Revert "Raise error on unknown primary key."
This reverts commit ee2be43.
1 parent fd8f0b2 commit 6474765

File tree

12 files changed

+18
-54
lines changed

12 files changed

+18
-54
lines changed

activerecord/lib/active_record/attribute_methods/primary_key.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ module ClassMethods
1414
# primary_key_prefix_type setting, though.
1515
def primary_key
1616
@primary_key ||= reset_primary_key
17-
raise ActiveRecord::UnknownPrimaryKey.new(self) unless @primary_key
18-
@primary_key
1917
end
2018

2119
# Returns a quoted version of the primary key name, used to construct SQL statements.
@@ -31,11 +29,6 @@ def reset_primary_key #:nodoc:
3129
key
3230
end
3331

34-
def primary_key? #:nodoc:
35-
@primary_key ||= reset_primary_key
36-
!@primary_key.nil?
37-
end
38-
3932
def get_primary_key(base_name) #:nodoc:
4033
return 'id' unless base_name && !base_name.blank?
4134

activerecord/lib/active_record/attribute_methods/read.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def define_method_attribute(attr_name)
4040
define_read_method(attr_name, attr_name, columns_hash[attr_name])
4141
end
4242

43-
if primary_key? && attr_name == primary_key && attr_name != "id"
43+
if attr_name == primary_key && attr_name != "id"
4444
define_read_method('id', attr_name, columns_hash[attr_name])
4545
end
4646
end
@@ -63,7 +63,7 @@ def define_read_method(method_name, attr_name, column)
6363
cast_code = column.type_cast_code('v')
6464
access_code = "(v=@attributes['#{attr_name}']) && #{cast_code}"
6565

66-
unless primary_key? && attr_name.to_s == primary_key.to_s
66+
unless attr_name.to_s == self.primary_key.to_s
6767
access_code.insert(0, "missing_attribute('#{attr_name}', caller) unless @attributes.has_key?('#{attr_name}'); ")
6868
end
6969

@@ -107,7 +107,7 @@ def read_attribute(attr_name)
107107

108108
def _read_attribute(attr_name)
109109
attr_name = attr_name.to_s
110-
attr_name = self.class.primary_key? && self.class.primary_key if attr_name == 'id'
110+
attr_name = self.class.primary_key if attr_name == 'id'
111111
value = @attributes[attr_name]
112112
unless value.nil?
113113
if column = column_for_attribute(attr_name)

activerecord/lib/active_record/attribute_methods/write.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def define_method_attribute=(attr_name)
1818
end
1919
end
2020

21-
if primary_key? && attr_name == primary_key && attr_name != "id"
21+
if attr_name == primary_key && attr_name != "id"
2222
generated_attribute_methods.module_eval("alias :id= :'#{primary_key}='")
2323
end
2424
end

activerecord/lib/active_record/base.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ def table_exists?
708708
# Returns an array of column objects for the table associated with this class.
709709
def columns
710710
if defined?(@primary_key)
711-
connection_pool.primary_keys[table_name] ||= @primary_key
711+
connection_pool.primary_keys[table_name] ||= primary_key
712712
end
713713

714714
connection_pool.columns[table_name]
@@ -953,7 +953,7 @@ def before_remove_const #:nodoc:
953953
# objects of different types from the same table.
954954
def instantiate(record)
955955
sti_class = find_sti_class(record[inheritance_column])
956-
record_id = sti_class.primary_key? && record[sti_class.primary_key]
956+
record_id = sti_class.primary_key && record[sti_class.primary_key]
957957

958958
if ActiveRecord::IdentityMap.enabled? && record_id
959959
if (column = sti_class.columns_hash[sti_class.primary_key]) && column.number?
@@ -1941,9 +1941,8 @@ def ensure_proper_type
19411941

19421942
# The primary key and inheritance column can never be set by mass-assignment for security reasons.
19431943
def self.attributes_protected_by_default
1944-
default = [ inheritance_column ]
1945-
default << primary_key if primary_key?
1946-
default << 'id' unless primary_key? && primary_key == 'id'
1944+
default = [ primary_key, inheritance_column ]
1945+
default << 'id' unless primary_key.eql? 'id'
19471946
default
19481947
end
19491948

activerecord/lib/active_record/errors.rb

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -169,18 +169,4 @@ def initialize(errors)
169169
@errors = errors
170170
end
171171
end
172-
173-
# Raised when a model attempts to fetch its primary key from the database, but the table
174-
# has no primary key declared.
175-
class UnknownPrimaryKey < ActiveRecordError
176-
attr_reader :model
177-
178-
def initialize(model)
179-
@model = model
180-
end
181-
182-
def message
183-
"Unknown primary key for table #{model.table_name} in model #{model}."
184-
end
185-
end
186172
end

activerecord/lib/active_record/fixtures.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ def table_rows
622622

623623
private
624624
def primary_key_name
625-
@primary_key_name ||= model_class && model_class.primary_key? && model_class.primary_key
625+
@primary_key_name ||= model_class && model_class.primary_key
626626
end
627627

628628
def has_primary_key_column?

activerecord/lib/active_record/persistence.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ def create
314314

315315
new_id = self.class.unscoped.insert attributes_values
316316

317-
self.id ||= new_id if self.class.primary_key?
317+
self.id ||= new_id if self.class.primary_key
318318

319319
IdentityMap.add(self) if IdentityMap.enabled?
320320
@new_record = false

activerecord/lib/active_record/relation.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Relation
1313

1414
# These are explicitly delegated to improve performance (avoids method_missing)
1515
delegate :to_xml, :to_yaml, :length, :collect, :map, :each, :all?, :include?, :to => :to_a
16-
delegate :table_name, :quoted_table_name, :primary_key, :primary_key?, :quoted_primary_key, :connection, :column_hash,:to => :klass
16+
delegate :table_name, :quoted_table_name, :primary_key, :quoted_primary_key, :connection, :column_hash,:to => :klass
1717

1818
attr_reader :table, :klass, :loaded
1919
attr_accessor :extensions, :default_scoped
@@ -36,7 +36,7 @@ def initialize(klass, table)
3636
def insert(values)
3737
primary_key_value = nil
3838

39-
if primary_key? && Hash === values
39+
if primary_key && Hash === values
4040
primary_key_value = values[values.keys.find { |k|
4141
k.name == primary_key
4242
}]
@@ -70,7 +70,7 @@ def insert(values)
7070
conn.insert(
7171
im,
7272
'SQL',
73-
primary_key? && primary_key,
73+
primary_key,
7474
primary_key_value,
7575
nil,
7676
binds)

activerecord/lib/active_record/transactions.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ def with_transaction_returning_status
303303
# Save the new record state and id of a record so it can be restored later if a transaction fails.
304304
def remember_transaction_record_state #:nodoc
305305
@_start_transaction_state ||= {}
306-
@_start_transaction_state[:id] = id if self.class.primary_key?
306+
@_start_transaction_state[:id] = id if has_attribute?(self.class.primary_key)
307307
unless @_start_transaction_state.include?(:new_record)
308308
@_start_transaction_state[:new_record] = @new_record
309309
end

activerecord/test/cases/attribute_methods/read_test.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@ def self.column_names
2424
def self.primary_key
2525
end
2626

27-
def self.primary_key?
28-
false
29-
end
30-
3127
def self.columns
3228
column_names.map { FakeColumn.new(name) }
3329
end

0 commit comments

Comments
 (0)