简体   繁体   English

使用Postgres对Rails区分大小写的应用程序

[英]Rails case sensitive applications with Postgres

I'm running a new Rails app on Postgresql and I do not understand why on Earth they made Postgreql case sensitive and didn't even make an option to turn this off. 我正在Postgresql上运行一个新的Rails应用程序,我不明白为什么在地球上他们使Postgreql区分大小写,甚至没有选择关闭它。

I mean, really, if someone registeres as "Sam" on my site, won't be able to log in as "sam", but there can be two different accounts "Sam" and "sam". 我的意思是,真的,如果有人在我的网站上注册为“Sam”,将无法以“sam”身份登录,但可以有两个不同的帐户“Sam”和“sam”。 This is a disaster, especially taking into consideration the fact that all other major databases are case insensitive. 这是一场灾难,特别是考虑到所有其他主要数据库都不区分大小写的事实。

Now instead of looking for a user like 现在,而不是寻找像这样的用户

User.find_by_name(params[:name])

I'll have to do this 我必须这样做

User.all(:conditions=>["name ILIKE ?", params[:name]]).first

I can't believe there's no way to avoid this in Rails because it destroys one of the main principles of the framework: database independence. 我无法相信在Rails中无法避免这种情况,因为它破坏了框架的一个主要原则:数据库独立性。

Is there a better way to implement case insensitive username/e-mail schema? 有没有更好的方法来实现不区分大小写的用户名/电子邮件架构?

There is a contrib module called citext which creates a case insensitive text type. 有一个名为citext的contrib模块,它创建一个不区分大小写的文本类型。 I think it's only included by default starting in pg 9.0. 我认为默认情况下它只包括在第9.0页开始。

Also, you could easily create a unique index to prevent sam, Sam, and SAM from having an account at the same time: 此外,您可以轻松创建唯一索引,以防止sam,Sam和SAM同时拥有一个帐户:

create table abc (users text); create table abc(用户文本); create unique index abc_users_ci on abc (upper(users)); 在abc上创建唯一索引abc_users_ci(upper(users)); insert into abc values ('sam'); 插入abc值('sam'); insert into abc values ('Sam'); 插入abc值('Sam'); ERROR: duplicate key value violates unique constraint "abc_users_ci" 错误:重复键值违反唯一约束“abc_users_ci”

tada! 田田! Or you could just complain that pgsql isn't like mysql. 或者你可能只是抱怨pgsql不像mysql。

I suppose you could have two DB columns, if preserving the case is important to you: 我想你可以有两个数据库列, 如果保留案例对你很重要:

  • One for the login name, which you will always convert to lower case when the user signs up, and which you use to login the user, after converting the name from the login form to lower case 一个用于登录名称,在用户登录后将始终转换为小写,并在将登录表单中的名称转换为小写后用于登录用户
  • One for the display name, which is what's get displayed as the username and is exactly what the user gave you at signup 一个用于显示名称,即显示为用户名的内容,正是用户在注册时提供的内容

especially taking into consideration the fact that all other major databases are case insensitive 特别考虑到所有其他主要数据库都不区分大小写的事实

That is simply not true. 那明显是错的。 Oracle, DB2 and Firebird are case-sensitive by default 默认情况下,Oracle,DB2和Firebird区分大小写

But I do agree, having such an option would make things a bit easier sometimes. 但我确实同意,有这样的选择会使事情有时更容易。

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

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