Adding comments to ambiguous DB columns

Posted . Visible to the public.

The DB schema is the most important source of truth for your application and should be very self-explanatory. If determining the true meaning of a DB column requires historical research in your issue tracker or reverse engineering of your source code you might consider adding a comment.

Both PostgreSQL and MySQL support comments in the DB schema:

class AddDetailsToProducts < ActiveRecord::Migration[8.0]
  def change
    add_column :products, :price, :decimal, precision: 8, scale: 2, comment: "Currency: USD"
  end
end

When to use this

  • Notate units for numbers
  • Add explanations to non-obvious boolean flags and timestamps
  • Maybe: Note that a field isn't used by all models of an STI Show archive.org snapshot -hierarchy

When not to use this

  • Any default columns: id, created_at, updated_at, foreign keys, type, lock_version
  • For columns with self-explanatory names within their table, e.g. users.first_name, users.email, etc.
Klaus Weidinger
Last edit
Klaus Weidinger
License
Source code in this card is licensed under the MIT License.
Posted by Klaus Weidinger to makandra dev (2025-06-23 15:26)