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
120
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
420
Service-Oriented Architecture
barunio
2
150
Other Decks in Programming
See All in Programming
CDKを使ったPagerDuty連携インフラのテンプレート化
shibuya_shogo
0
130
Rails 1.0 のコードで学ぶ find_by* と method_missing の仕組み / Learn how find_by_* and method_missing work in Rails 1.0 code
maimux2x
1
270
Webフレームワークとともに利用するWeb components / JSConf.jp おかわり
spring_raining
1
150
PEPCは何を変えようとしていたのか
ken7253
3
320
AIプログラミング雑キャッチアップ
yuheinakasaka
21
5.4k
Django NinjaによるAPI開発の効率化とリプレースの実践
kashewnuts
1
310
TCAを用いたAmebaのリアーキテクチャ
dazy
0
240
推しメソッドsource_locationのしくみを探る - はじめてRubyのコードを読んでみた
nobu09
2
370
複数のAWSアカウントから横断で 利用する Lambda Authorizer の作り方
tc3jp
0
130
Generating OpenAPI schema from serializers throughout the Rails stack - Kyobashi.rb #5
envek
1
440
Better Code Design in PHP
afilina
0
190
Jakarta EE meets AI
ivargrimstad
0
790
Featured
See All Featured
Faster Mobile Websites
deanohume
306
31k
Building Adaptive Systems
keathley
40
2.4k
Building Applications with DynamoDB
mza
93
6.3k
Fashionably flexible responsive web design (full day workshop)
malarkey
406
66k
A designer walks into a library…
pauljervisheath
205
24k
[RailsConf 2023] Rails as a piece of cake
palkan
53
5.3k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
12k
Stop Working from a Prison Cell
hatefulcrawdad
268
20k
BBQ
matthewcrist
87
9.5k
Practical Orchestrator
shlominoach
186
10k
Optimizing for Happiness
mojombo
377
70k
GraphQLの誤解/rethinking-graphql
sonatard
69
10k
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]