Are you a novice or intermediate web developer, starting a brand new project that uses a database? Not sure where to begin in terms of your table design (schema) or ORM models? I've advised quite a few students on this exact topic before, so I assembled these tips as a "check list" to use when you are designing your database:
Book
, Author
, BlogPost
, etc). The methods (functions) should be the "verbs" (e.g. get_latest
or update_messages
) and the properties (columns) should be adjectives or relationships (e.g. popularity
or color
for adjectives, or creator
for a relationship)category
field to distinguish them. Another example: Don't have books_romance
and books_horror
as your tables, instead just have a books
table and have genre
be a column.Tweet
might have many User
author -- but unless that feature is a "must have" for your product now, start with each Tweet
having only one User
(One to Many or 1:n) until you finish other aspects of your software. Keep your product's design short and sweet. Most software products won't ever even get used, let alone improved upon.Bonus (Django specific) rule: Avoid "Generic Foreign Keys" if you can!
Background: A student recently asked me what is the best practice for designing data models. While the question was originally for the Django ORM on PostgreSQL, I've given the exact same advice while teaching classes on JavaScript / Node / Express stack using SQLize and MySQL, and in another course, Mongoose for MongoDB. While I've never taught a class on Ruby on Rails, the same advice also goes for ActiveRecord. These tips are pretty universal! In fact, as a Star Trek fan, I'll point out that even Star Fleet needs good database design:
Michael is a software developer and instructor. His favorite teaching challenge is teaching big computer science concepts to total newbies. He believes that complex concepts don't need to be complicated! His favorite engineering approach is full stack thinking: recognizing complexity that can be shifted between low-level infrastructure and JavaScript, or vice-versa. When he's not coding, he's probably doing political work, playing video games that feel too much like work, and counting down the days until first contact.