Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Using Database Constraints Wisely

Barun Singh
December 13, 2011

Using Database Constraints Wisely

Barun Singh

December 13, 2011
Tweet

More Decks by Barun Singh

Other Decks in Programming

Transcript

  1. What is a database? A collection of data that you

    can read, write, and organize The software tools that let you do this
  2. What do we want from a database? It should be

    … •  Reliable •  Fast •  Secure •  …
  3. What do we want from a relational database? It should

    be … •  Reliable •  Fast •  Secure •  Sensible •  …
  4. The Rails way says… •  You should be database agnostic

    •  Application layer is the only really important part
  5. What’s the worst that can happen? •  Feature doesn’t work

    •  Site goes down •  Irrecoverable data loss
  6. I write tests. Why bother with database constraints? I test

    my entire application flow through integration tests. Why write unit tests?
  7. I write tests. Why bother with database constraints? My application

    requires Javascript, and I have JS validations. Why bother with model-level validations?
  8. 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?
  9. Polymorpheus The gem takes care of all of this for

    you through a simple migration method
  10. Polymorpheus The gem takes care of all of this for

    you through a simple migration method