简体   繁体   English

Rails validates_uniqueness_of似乎对我不起作用

[英]Rails validates_uniqueness_of doesn't seem to be working for me

I'm having a hard time trying to avoid duplicates in my database and really curious to know if I'm actually doing this correctly. 我很难避免数据库中出现重复项,我真的很想知道我是否正确地做到了这一点。 In the example below, my technical reports actually work perfectly fine. 在下面的示例中,我的技术报告实际上运行良好。 So there's no duplicate nodes in the technical report, even if I try to create a duplicate manually. 因此,即使我尝试手动创建重复项,技术报告中也没有重复的节点。

However, I can't say the same for the nodes' vulns. 但是,对于节点的外伤我不能说同样的话。 When I import the exact same file (containing data) twice, I get duplicate Vulns for the same IP when I believe it's not supposed to happen. 当我两次导入完全相同的文件(包含数据)时,如果我认为不应该发生相同的IP,则会得到重复的Vulns。

So here are two models that I have: 因此,这里有两个模型:

#app/models/node.rb
class Node < ActiveRecord::Base
    validates_uniqueness_of :technical_report_id, :scope => :ip    
    has_many :vulns, dependent: :destroy
end

.

#app/models/vuln.rb
class Vuln < ActiveRecord::Base
  validates_uniqueness_of :node_id, :scope => [:master_finding_id, :vuln_finding_id, :additional_output, :port]

  belongs_to :node
  belongs_to :master_finding
  belongs_to :vuln_finding
end

However, when I go to importing data, I still find myself with duplicates in the Vuln table. 但是,当我要导入数据时,我仍然在Vuln表中发现自己重复。 I've used rails c to validate this as well. 我也使用过rails c来验证这一点。

irb(main):012:0> Vuln.where(node_id: 12).pluck(:master_finding_id, :additional_output, :vuln_finding_id).length
   (0.4ms)  SELECT `vulns`.`master_finding_id`, `vulns`.`additional_output`, `vulns`.`vuln_finding_id` FROM `vulns` WHERE `vulns`.`node_id` = 12
=> 2

When I go to call .uniq it shows that there's only one entry. 当我打电话给.uniq它表明只有一个条目。

irb(main):013:0> Vuln.where(node_id: 12).pluck(:master_finding_id, :additional_output, :vuln_finding_id).uniq.length
   (0.5ms)  SELECT `vulns`.`master_finding_id`, `vulns`.`additional_output`, `vulns`.`vuln_finding_id` FROM `vulns` WHERE `vulns`.`node_id` = 12
=> 1

Does anyone know what I'm doing wrong here? 有人知道我在做什么错吗? I'm not sure why this works for one model and not the other one. 我不确定为什么这适用于一种模型而不适用于另一种模型。 If I try to create two of the exact same Vuln records from rails c CLI, it rolls back like it should have, but not when it's created otherwise. 如果我尝试从rails c CLI创建两个完全相同的Vuln记录,它将回滚到应有的水平,但在创建时会回滚。

EDIT 编辑

Looks like my issue is with activerecord-import instead. 看起来我的问题是用activerecord-import代替的。 I'm not sure why but it imports nodes just fine, but it calls the "Class Create Many Without Validations Or Callbacks" when it goes to importing vulns. 我不知道为什么,但是它可以很好地导入节点,但是在导入外阴时,它称为“无需验证或回调就创建许多类”。 Guess because there's a lot more data or something. 猜猜是因为有更多的数据或其他东西。 I think I have this sort of figured out for now. 我想我现在已经有了这种想法。 Perhaps I just need to write a quick method to validate the uniqueness manually since I don't want to get rid of the mass importing gem. 也许我只需要编写一种快速方法来手动验证唯一性,因为我不想摆脱大量导入的gem的麻烦。

validates_uniqueness_of can not ensure uniqueness in every case. validates_uniqueness_of不能确保每种情况下的唯一性。 Within the docs you can see an example for the race condition within this kind of validation. 文档中,您可以在这种验证中看到竞争条件的示例。 If it is realy needed you should also use an uniq constraint on database level. 如果确实需要,还应该在数据库级别使用uniq约束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM