一个的has_many货币,其中B没有主键 [英] A has_many Bs where B has no primary key

查看:81
本文介绍了一个的has_many货币,其中B没有主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有型号A和B;一个的has_many B,和B belongs_to的A.到目前为止,一切都很好,只是,我指定B没有主键。我不打算永远修改或删除个别B排灯,我期望有几百万给他们十亿,因此省略了主键会非常方便,空间明智的。

I've got models A and B; A has_many B, and B belongs_to A. So far, so good—except, I specify that B doesn't have a primary key. I don't plan to ever modify or delete individual B rows, and I expect to have several million to billion of them, so omitting the primary key will be really handy, space-wise.

创建B的表的迁移是这样的:

The migration to create B's table looked like this:

class CreateBs < ActiveRecord::Migration
  def change
    create_table :bs, {:id => false} do |t|
      # … rest of fields …
    end
  end
end

不幸的是,ActiveRecord的不同意;试图创建一个A(!这是正确的)结果是:

Unfortunately, ActiveRecord disagrees; trying to create an 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 the exception, its message states:

"Unknown primary key for table bs in model B." 

(这将是因为B没有主键。)

(which would be because B has no primary key.)

我想没有这个问题!有什么办法?

I'd like to not have this issue! Is there any way?

推荐答案

的罪魁祸首竟然是一个从细节丢失的问题,当然,和我的工作记忆丢失:

The culprit turned out to be a detail missing from the question—of course—and missing from my working memory:

class A < ActiveRecord::Base
  has_many :bs

  validates :bs, :presence => true
end

虽然我认为一无所知的时候,有,在许多其他验证-A的 BS 的presence验证。如果你眯着眼睛真的很难在全回溯,我是有益的,足以在我原来的问题截断,你会看到:

Though I thought nothing about it at the time, there was—among many other validations—a validation of the presence of bs. If you squint really hard at the full backtrace—which I was "helpful" enough to truncate in my original question, you see:

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'
        from /Users/annelicuss/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/associations/collection_proxy.rb:87:in `method_missing'
        from /Users/annelicuss/.rvm/gems/ruby-1.9.3-p194/gems/activemodel-3.2.8/lib/active_model/errors.rb:255:in `block in add_on_blank'
        from /Users/annelicuss/.rvm/gems/ruby-1.9.3-p194/gems/activemodel-3.2.8/lib/active_model/errors.rb:253:in `each'
        from /Users/annelicuss/.rvm/gems/ruby-1.9.3-p194/gems/activemodel-3.2.8/lib/active_model/errors.rb:253:in `add_on_blank'
        from /Users/annelicuss/.rvm/gems/ruby-1.9.3-p194/gems/activemodel-3.2.8/lib/active_model/validations/presence.rb:8:in `validate'
-------------------------------------------------------------------------------------------------^^^^^^^^^^^^^^^^^^^^^^^

发生在 active_model /验证/ presence.rb 的错误!删除此约束是足以让我们不断前进。它不能找到验证的目标,因为它试图使用主键这样做,因此失败坏

The error occurs in active_model/validations/presence.rb! Removing this constraint is enough to get us going. It can't find the target of the validation, as it tries to use the primary key to do so, and hence fails bad.

这篇关于一个的has_many货币,其中B没有主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆