$30 off During Our Annual Pro Sale. View Details »
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
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
460
Service-Oriented Architecture
barunio
2
160
Other Decks in Programming
See All in Programming
20 years of Symfony, what's next?
fabpot
2
360
「コードは上から下へ読むのが一番」と思った時に、思い出してほしい話
panda728
PRO
38
26k
AIコードレビューがチームの"文脈"を 読めるようになるまで
marutaku
0
350
組み合わせ爆発にのまれない - 責務分割 x テスト
halhorn
1
150
Integrating WordPress and Symfony
alexandresalome
0
150
ゲームの物理 剛体編
fadis
0
340
大体よく分かるscala.collection.immutable.HashMap ~ Compressed Hash-Array Mapped Prefix-tree (CHAMP) ~
matsu_chara
2
220
Microservices rules: What good looks like
cer
PRO
0
1.4k
How Software Deployment tools have changed in the past 20 years
geshan
0
29k
堅牢なフロントエンドテスト基盤を構築するために行った取り組み
shogo4131
8
2.3k
認証・認可の基本を学ぼう前編
kouyuume
0
200
Socio-Technical Evolution: Growing an Architecture and Its Organization for Fast Flow
cer
PRO
0
330
Featured
See All Featured
Balancing Empowerment & Direction
lara
5
800
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
1.8k
How To Stay Up To Date on Web Technology
chriscoyier
791
250k
Designing for humans not robots
tammielis
254
26k
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
The Illustrated Children's Guide to Kubernetes
chrisshort
51
51k
Measuring & Analyzing Core Web Vitals
bluesmoon
9
710
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
3.2k
Thoughts on Productivity
jonyablonski
73
5k
How STYLIGHT went responsive
nonsquared
100
6k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
22k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.1k
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]