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
なぜSQLはAIぽく見えるのか/why does SQL look AI like
florets1
0
340
16年目のピクシブ百科事典を支える最新の技術基盤 / The Modern Tech Stack Powering Pixiv Encyclopedia in its 16th Year
ahuglajbclajep
5
920
それ、本当に安全? ファイルアップロードで見落としがちなセキュリティリスクと対策
penpeen
7
2.3k
GISエンジニアから見たLINKSデータ
nokonoko1203
0
190
Combinatorial Interview Problems with Backtracking Solutions - From Imperative Procedural Programming to Declarative Functional Programming - Part 2
philipschwarz
PRO
0
150
【卒業研究】会話ログ分析によるユーザーごとの関心に応じた話題提案手法
momok47
0
170
IFSによる形状設計/デモシーンの魅力 @ 慶應大学SFC
gam0022
1
260
例外処理とどう使い分ける?Result型を使ったエラー設計 #burikaigi
kajitack
16
5.7k
責任感のあるCloudWatchアラームを設計しよう
akihisaikeda
3
120
AI Agent の開発と運用を支える Durable Execution #AgentsInProd
izumin5210
7
2.2k
Implementation Patterns
denyspoltorak
0
260
大規模Cloud Native環境におけるFalcoの運用
owlinux1000
0
260
Featured
See All Featured
Breaking role norms: Why Content Design is so much more than writing copy - Taylor Woolridge
uxyall
0
140
Site-Speed That Sticks
csswizardry
13
1k
Facilitating Awesome Meetings
lara
57
6.7k
Writing Fast Ruby
sferik
630
62k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
231
22k
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
400
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
9.8k
The Invisible Side of Design
smashingmag
302
51k
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
58
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
1.8k
Code Reviewing Like a Champion
maltzj
527
40k
The Curse of the Amulet
leimatthew05
1
7.6k
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]