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
110
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
350
Service-Oriented Architecture
barunio
2
140
Other Decks in Programming
See All in Programming
Anthropic Cookbook のおすすめレシピ
schroneko
7
980
AWS CDKコントリビュートTIPS / aws-cdk-contribution-tips
gotok365
2
200
Random\Randomizer クラスで日常のあれこれを解決しよう! / Random\Randomizer class solves familiar trouble
cocoeyes02
0
250
[技育CAMPアカデミア]アイディアを形に!【超入門】スマホアプリ開発〜リリースまでの流れをご紹介
teamlab
PRO
0
380
大規模UIKitベースアプリへのTCAの段階的導入/gradual-adoption-of-tca-in-a-large-scale-uikit-based-app
takehilo
1
180
『Railsオワコン』と言われる時代に、なぜブルーモ証券はRailsを選ぶのか
free_world21
0
250
Node.js v22 で変わること
yosuke_furukawa
PRO
9
3.5k
try!Swift Tokyo 2024 参加報告 LT
akidon0000
1
220
Azure OpenAI Serviceのプロンプトエンジニアリング入門
tomokusaba
3
710
障害対応を起点としたもっといい開発と運用のサイクル作りのためにできること / Hatena Enginner Seminar #29
polamjag
0
180
StoreKit2によるiOSのアプリ内課金のリニューアル
kangnux
0
110
GitHub Copilotのススメ
marcy731
1
200
Featured
See All Featured
Build your cross-platform service in a week with App Engine
jlugia
225
17k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
25
2.3k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
21
1.6k
What's in a price? How to price your products and services
michaelherold
237
11k
5 minutes of I Can Smell Your CMS
philhawksworth
199
19k
Visualization
eitanlees
136
14k
Web Components: a chance to create the future
zenorocha
305
41k
Designing with Data
zakiwarfel
96
4.8k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
322
20k
Building Your Own Lightsaber
phodgson
99
5.7k
Done Done
chrislema
178
15k
Building Effective Engineering Teams - LeadDev
addyosmani
28
1.8k
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]