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
1
130
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
470
Service-Oriented Architecture
barunio
2
160
Other Decks in Programming
See All in Programming
0→1 フロントエンド開発 Tips🚀 #レバテックMeetup
bengo4com
0
510
CSC307 Lecture 05
javiergs
PRO
0
480
Oxlintはいいぞ
yug1224
5
980
Basic Architectures
denyspoltorak
0
600
Patterns of Patterns
denyspoltorak
0
1.3k
Fluid Templating in TYPO3 14
s2b
0
110
rack-attack gemによるリクエスト制限の失敗と学び
pndcat
0
250
MDN Web Docs に日本語翻訳でコントリビュート
ohmori_yusuke
0
590
Spinner 軸ズレ現象を調べたらレンダリング深淵に飲まれた #レバテックMeetup
bengo4com
1
220
CSC307 Lecture 02
javiergs
PRO
1
770
Graviton と Nitro と私
maroon1st
0
170
Kotlin Multiplatform Meetup - Compose Multiplatform 외부 의존성 아키텍처 설계부터 운영까지
wisemuji
0
180
Featured
See All Featured
How to train your dragon (web standard)
notwaldorf
97
6.5k
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
150
For a Future-Friendly Web
brad_frost
181
10k
HDC tutorial
michielstock
1
330
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.1k
Tips & Tricks on How to Get Your First Job In Tech
honzajavorek
0
420
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
Discover your Explorer Soul
emna__ayadi
2
1.1k
Google's AI Overviews - The New Search
badams
0
890
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.3k
Believing is Seeing
oripsolob
1
36
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]