mirror of
https://github.com/jbranchaud/til
synced 2026-01-09 01:58:02 +00:00
Add Storing Emails With citext as a postgres til
This commit is contained in:
@@ -7,7 +7,7 @@ variety of languages and technologies. These are things that don't really
|
|||||||
warrant a full blog post. These are mostly things I learn by pairing with
|
warrant a full blog post. These are mostly things I learn by pairing with
|
||||||
smart people at [Hashrocket](http://hashrocket.com/).
|
smart people at [Hashrocket](http://hashrocket.com/).
|
||||||
|
|
||||||
_376 TILs and counting..._
|
_377 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -206,6 +206,7 @@ _376 TILs and counting..._
|
|||||||
- [Sets With The Values Command](postgres/sets-with-the-values-command.md)
|
- [Sets With The Values Command](postgres/sets-with-the-values-command.md)
|
||||||
- [Sleeping](postgres/sleeping.md)
|
- [Sleeping](postgres/sleeping.md)
|
||||||
- [Special Math Operators](postgres/special-math-operators.md)
|
- [Special Math Operators](postgres/special-math-operators.md)
|
||||||
|
- [Storing Emails With citext](postgres/storing-emails-with-citext.md)
|
||||||
- [String Contains Another String](postgres/string-contains-another-string.md)
|
- [String Contains Another String](postgres/string-contains-another-string.md)
|
||||||
- [Temporarily Disable Triggers](postgres/temporarily-disable-triggers.md)
|
- [Temporarily Disable Triggers](postgres/temporarily-disable-triggers.md)
|
||||||
- [Temporary Tables](postgres/temporary-tables.md)
|
- [Temporary Tables](postgres/temporary-tables.md)
|
||||||
|
|||||||
35
postgres/storing-emails-with-citext.md
Normal file
35
postgres/storing-emails-with-citext.md
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
# Storing Emails With citext
|
||||||
|
|
||||||
|
Email addresses should be treated as case-insensitive because they are. If a
|
||||||
|
user is trying to sign in with their email address, we shouldn't care if
|
||||||
|
they type `user@example.com` or `User@example.com`. Both of those email
|
||||||
|
addresses should be treated as equal and ultimately lead us to the same
|
||||||
|
`User` record.
|
||||||
|
|
||||||
|
With the
|
||||||
|
[`citext`](http://www.postgresql.org/docs/current/static/citext.html)
|
||||||
|
extension, we can create a column that acts as a case-insensitive text type.
|
||||||
|
Any comparisons on a column of that type will internally have the `lower`
|
||||||
|
function executed on the arguments.
|
||||||
|
|
||||||
|
The following example shows this in action:
|
||||||
|
|
||||||
|
```sql
|
||||||
|
create extension if not exists citext;
|
||||||
|
|
||||||
|
create table citext_emails (
|
||||||
|
id serial primary key,
|
||||||
|
email citext not null unique
|
||||||
|
);
|
||||||
|
|
||||||
|
insert into citext_emails (email) values ('LizLemon@nbc.com');
|
||||||
|
|
||||||
|
select * from citext_emails where email = 'lizlemon@nbc.com';
|
||||||
|
-- id | email
|
||||||
|
-- ----+------------------
|
||||||
|
-- 1 | LizLemon@nbc.com
|
||||||
|
```
|
||||||
|
|
||||||
|
See
|
||||||
|
[`citext-emails.sql`](https://github.com/jbranchaud/postgresing/blob/master/citext-emails.sql)
|
||||||
|
for a full example.
|
||||||
Reference in New Issue
Block a user