Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Using Database Constraints Wisely
Search
Barun Singh
December 13, 2011
Programming
0
120
Using Database Constraints Wisely
Barun Singh
December 13, 2011
Tweet
Share
More Decks by Barun Singh
See All by Barun Singh
Atomic Commits
barunio
1
410
Service-Oriented Architecture
barunio
2
150
Other Decks in Programming
See All in Programming
StarlingMonkeyを触ってみた話 - 2024冬
syumai
3
310
KMP와 kotlinx.rpc로 서버와 클라이언트 동기화
kwakeuijin
0
250
技術的負債と向き合うカイゼン活動を1年続けて分かった "持続可能" なプロダクト開発
yuichiro_serita
0
180
Amazon S3 NYJavaSIG 2024-12-12
sullis
0
110
PHPUnitしか使ってこなかった 一般PHPerがPestに乗り換えた実録
mashirou1234
0
360
EC2からECSへ 念願のコンテナ移行と巨大レガシーPHPアプリケーションの再構築
sumiyae
2
490
Внедряем бюджетирование, или Как сделать хорошо?
lamodatech
0
780
create_tableをしただけなのに〜囚われのuuid編〜
daisukeshinoku
0
310
命名をリントする
chiroruxx
1
490
htmxって知っていますか?次世代のHTML
hiro_ghap1
0
360
どうして手を動かすよりもチーム内のコードレビューを優先するべきなのか
okashoi
3
690
Compose UIテストを使った統合テスト
hiroaki404
0
110
Featured
See All Featured
YesSQL, Process and Tooling at Scale
rocio
170
14k
The Power of CSS Pseudo Elements
geoffreycrofte
73
5.4k
How to Think Like a Performance Engineer
csswizardry
22
1.2k
A better future with KSS
kneath
238
17k
Rails Girls Zürich Keynote
gr2m
94
13k
Being A Developer After 40
akosma
89
590k
Reflections from 52 weeks, 52 projects
jeffersonlam
347
20k
Imperfection Machines: The Place of Print at Facebook
scottboms
266
13k
Practical Orchestrator
shlominoach
186
10k
Thoughts on Productivity
jonyablonski
68
4.4k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
29
2k
Docker and Python
trallard
43
3.2k
Transcript
Using database constraints wisely Barun Singh
[email protected]
Founder & CTO
What is a database? A collection of data that you
can read, write, and organize The software tools that let you do this
What do we want from a database? It should be
… • Reliable • Fast • Secure • …
A relational database is not the same as a generic
data store
A relational database is not the same as a generic
data store
What do we want from a relational database? It should
be … • Reliable • Fast • Secure • Sensible • …
Wait a minute… Is this Rails approved™ ?
The Rails way says… • You should be database agnostic
• Application layer is the only really important part
The database is important
But why? Because you make mistakes.
What’s the worst that can happen? • Feature doesn’t work
• Site goes down • Irrecoverable data loss
I write tests. Why bother with database constraints? I test
my entire application flow through integration tests. Why write unit tests?
I write tests. Why bother with database constraints? My application
requires Javascript, and I have JS validations. Why bother with model-level validations?
Your database has an interface.
How? • Simple constraints: [not null] • Unique indices •
Foreign keys [foreigner gem]
It’s not always that simple
Polymorphism Imageable Picture Employee Product
Polymorphism What does this reference?
Polymorphism
Polymorpheus
A couple little snags… How can you make sure that
one picture has only an employee_id or product_id but not both? (xor constraints are tough) And what if the polymorphic relationship has to be unique?
Polymorpheus The gem takes care of all of this for
you through a simple migration method
Polymorpheus The gem takes care of all of this for
you through a simple migration method
Polymorpheus
Polymorpheus Also provides a validation method, model level validations are
still important
You can keep your application logic clean and set up
your database properly
Questions?
[email protected]