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
450
Service-Oriented Architecture
barunio
2
150
Other Decks in Programming
See All in Programming
3年ぶりにコードを書いた元CTOが Claude Codeと30分でMVPを作った話
maikokojima
0
620
PHPに関数型の魂を宿す〜PHP 8.5 で実現する堅牢なコードとは〜 #phpcon_hiroshima / phpcon-hiroshima-2025
shogogg
1
320
スキーマ駆動で、Zod OpenAPI Honoによる、API開発するために、Hono Takibiというライブラリを作っている
nakita628
0
300
CSC305 Lecture 08
javiergs
PRO
0
270
その面倒な作業、「Dart」にやらせませんか? Flutter開発者のための業務効率化
yordgenome03
1
140
When Dependencies Fail: Building Antifragile Applications in a Fragile World
selcukusta
0
110
バッチ処理を「状態の記録」から「事実の記録」へ
panda728
PRO
0
180
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
390
「ちょっと古いから」って避けてた技術書、今だからこそ読もう
mottyzzz
11
7k
CSC305 Lecture 10
javiergs
PRO
0
220
Software Architecture
hschwentner
6
2.3k
Server Side Kotlin Meetup vol.16: 内部動作を理解して ハイパフォーマンスなサーバサイド Kotlin アプリケーションを書こう
ternbusty
3
230
Featured
See All Featured
Art, The Web, and Tiny UX
lynnandtonic
303
21k
4 Signs Your Business is Dying
shpigford
185
22k
YesSQL, Process and Tooling at Scale
rocio
173
15k
Build your cross-platform service in a week with App Engine
jlugia
232
18k
BBQ
matthewcrist
89
9.8k
Git: the NoSQL Database
bkeepers
PRO
431
66k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
17k
Fireside Chat
paigeccino
40
3.7k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
285
14k
Rails Girls Zürich Keynote
gr2m
95
14k
KATA
mclloyd
PRO
32
15k
Learning to Love Humans: Emotional Interface Design
aarron
274
41k
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]