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
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
440
Service-Oriented Architecture
barunio
2
150
Other Decks in Programming
See All in Programming
複数アプリケーションを育てていくための共通化戦略
irof
10
3.9k
Cline指示通りに動かない? AI小説エージェントで学ぶ指示書の書き方と自動アップデートの仕組み
kamomeashizawa
1
540
Team topologies and the microservice architecture: a synergistic relationship
cer
PRO
0
860
C++20 射影変換
faithandbrave
0
490
無関心の谷
kanayannet
0
180
Select API from Kotlin Coroutine
jmatsu
1
180
GraphRAGの仕組みまるわかり
tosuri13
7
430
データの民主化を支える、透明性のあるデータ利活用への挑戦 2025-06-25 Database Engineering Meetup#7
y_ken
0
220
業務自動化をJavaとSeleniumとAWS Lambdaで実現した方法
greenflagproject
1
120
Practical Tips and Tricks for Working with Compose Multiplatform Previews (mDevCamp 2025)
stewemetal
0
130
Beyond Portability: Live Migration for Evolving WebAssembly Workloads
chikuwait
0
380
Passkeys for Java Developers
ynojima
3
870
Featured
See All Featured
Mobile First: as difficult as doing things right
swwweet
223
9.7k
Product Roadmaps are Hard
iamctodd
PRO
53
11k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
123
52k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
15
1.5k
The Cost Of JavaScript in 2023
addyosmani
50
8.4k
A better future with KSS
kneath
239
17k
Practical Orchestrator
shlominoach
188
11k
Embracing the Ebb and Flow
colly
86
4.7k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
667
120k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.5k
Into the Great Unknown - MozCon
thekraken
39
1.8k
Fantastic passwords and where to find them - at NoRuKo
philnash
51
3.3k
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]