(NULL) 中的 NULL 未正确匹配
使用 Rails 3 主动关系,我有一个范围:
scope :duplicate_contact, lambda {|contact| where(
:person_id => contact.person_id,
:salutation => contact.salutation,
:first_name => contact.first_name,
:last_name => contact.last_name,
:suffix => contact.suffix,
:birthday => contact.birthday,
:address => contact.address,
:city => contact.city,
:state => contact.state,
:zip => contact.zip,
:phone_1 => [contact.phone_1,contact.phone_2,contact.phone_3],
:phone_1_type => [contact.phone_1_type,contact.phone_2_type,contact.phone_3_type],
:phone_2 => [contact.phone_1,contact.phone_2,contact.phone_3],
:phone_2_type => [contact.phone_1_type,contact.phone_2_type,contact.phone_3_type],
:phone_3 => [contact.phone_1,contact.phone_2,contact.phone_3],
:phone_3_type => [contact.phone_1_type,contact.phone_2_type,contact.phone_3_type],
:email => [contact.email,contact.alternate_email],
:alternate_email => [contact.email,contact.alternate_email]
)
}
当 :email 为 NULL 时,这会出现问题。它返回零行,而实际上它应该返回至少 1 行,即 duplicate_contact(contact).size == 0
为 true,而它应该为 false。
我认为这与 mysql 文档中的这句话有关:“在 SQL 中,与任何其他值(甚至 NULL)相比,NULL 值永远不是真的。”
我怎样才能让它返回正确的结果?
Using Rails 3 active relation, I have a scope:
scope :duplicate_contact, lambda {|contact| where(
:person_id => contact.person_id,
:salutation => contact.salutation,
:first_name => contact.first_name,
:last_name => contact.last_name,
:suffix => contact.suffix,
:birthday => contact.birthday,
:address => contact.address,
:city => contact.city,
:state => contact.state,
:zip => contact.zip,
:phone_1 => [contact.phone_1,contact.phone_2,contact.phone_3],
:phone_1_type => [contact.phone_1_type,contact.phone_2_type,contact.phone_3_type],
:phone_2 => [contact.phone_1,contact.phone_2,contact.phone_3],
:phone_2_type => [contact.phone_1_type,contact.phone_2_type,contact.phone_3_type],
:phone_3 => [contact.phone_1,contact.phone_2,contact.phone_3],
:phone_3_type => [contact.phone_1_type,contact.phone_2_type,contact.phone_3_type],
:email => [contact.email,contact.alternate_email],
:alternate_email => [contact.email,contact.alternate_email]
)
}
This has a problem when :email is NULL. It returns back zero rows, when in fact it should return at least 1 row, ie duplicate_contact(contact).size == 0
is true when it should be false.
I think this has to do with this staement from the mysql docs: "In SQL, the NULL value is never true in comparison to any other value, even NULL."
How can I get this to return the correct result?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现的一种可能的解决方案:
One possible solution that I found: