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
リッチエディターを安全に開発・運用するために
unachang113
1
270
CDK引数設計道場100本ノック
badmintoncryer
2
590
抽象化という思考のツール - 理解と活用 - / Abstraction-as-a-Tool-for-Thinking
shin1x1
1
860
Claude Code派?Gemini CLI派? みんなで比較LT会!_20250716
junholee
1
750
AI Ramen Fight
yusukebe
0
120
Android 16KBページサイズ対応をはじめからていねいに
mine2424
0
740
QA x AIエコシステム段階構築作戦
osu
0
210
バイブコーディング超えてバイブデプロイ〜CloudflareMCPで実現する、未来のアプリケーションデリバリー〜
azukiazusa1
2
730
オンコール⼊⾨〜ページャーが鳴る前に、あなたが備えられること〜 / Before The Pager Rings
yktakaha4
2
1.2k
テスターからテストエンジニアへ ~新米テストエンジニアが歩んだ9ヶ月振り返り~
non0113
2
240
202507_ADKで始めるエージェント開発の基本 〜デモを通じて紹介〜(奥田りさ)The Basics of Agent Development with ADK — A Demo-Focused Introduction
risatube
PRO
5
1.2k
[Codecon - 2025] Como não odiar seus testes
camilacampos
0
100
Featured
See All Featured
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
15
1.6k
The Cost Of JavaScript in 2023
addyosmani
51
8.6k
Building Adaptive Systems
keathley
43
2.7k
A Modern Web Designer's Workflow
chriscoyier
695
190k
A designer walks into a library…
pauljervisheath
207
24k
VelocityConf: Rendering Performance Case Studies
addyosmani
332
24k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
8
720
Bootstrapping a Software Product
garrettdimon
PRO
307
110k
Measuring & Analyzing Core Web Vitals
bluesmoon
7
530
Being A Developer After 40
akosma
90
590k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
31
1.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]