I have models A and B; A has_many B and B belongs to A. So far, except as well, I point out that B does not have a primary key. I do not plan to ever change or delete individual lines of B, and I expect that there will be from several million to a billion, so omitting the primary key will be very convenient, indirectly.
The transition to creating table B was as follows:
class CreateBs < ActiveRecord::Migration
def change
create_table :bs, {:id => false} do |t|
end
end
end
Unfortunately, ActiveRecord does not agree; trying to create A (that's right!) results in:
1.9.3p194 :001 > A.create!
(0.3ms) BEGIN
(0.1ms) ROLLBACK
ActiveRecord::UnknownPrimaryKey: ActiveRecord::UnknownPrimaryKey
from /Users/annelicuss/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/reflection.rb:366:in `primary_key'
from /Users/annelicuss/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/reflection.rb:216:in `association_primary_key'
from /Users/annelicuss/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/associations/has_many_association.rb:104:in `foreign_key_present?'
from /Users/annelicuss/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/associations/association.rb:165:in `find_target?'
from /Users/annelicuss/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/associations/collection_association.rb:332:in `load_target'
from /Users/annelicuss/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/associations/collection_proxy.rb:44:in `load_target'
…
If you catch an exception, it messagestates:
"Unknown primary key for table bs in model B."
(This is because B does not have a primary key.)
I would not want to have this problem! Is there any way?
source
share