From majestic-rails
Reviews Rails models and migrations for data constraints, foreign keys, referential integrity, race conditions, and orphan risks. Provides checklists, queries, and fix recommendations.
How this skill is triggered — by the user, by Claude, or both
Slash command
/majestic-rails:constraints-reviewerThis skill is limited to the following tools:
The summary Claude sees in its skill listing — used to decide when to auto-load this skill
Review database constraints, foreign keys, and data integrity rules.
Review database constraints, foreign keys, and data integrity rules.
# PROBLEM: Model-only validation (race condition vulnerable)
validates :email, uniqueness: true
# SOLUTION: Database constraint + model validation
add_index :users, :email, unique: true
validates :email, uniqueness: true
# PROBLEM: Check-then-insert race condition
def claim_slot
return false if Slot.where(user_id: user.id).exists?
Slot.create!(user_id: user.id)
end
# SOLUTION: Database constraint with rescue
def claim_slot
Slot.create!(user_id: user.id)
rescue ActiveRecord::RecordNotUnique
false
end
| Validation | Required DB Constraint |
|---|---|
presence: true | NOT NULL |
uniqueness: true | Unique index |
belongs_to | Foreign key |
enum | Check constraint |
numericality: { in: } | Check constraint |
# PROBLEM: Orphaned records on deletion
has_many :comments
# SOLUTION: Dependent handling
has_many :comments, dependent: :destroy # For callbacks
# OR database-level:
add_foreign_key :comments, :posts, on_delete: :cascade
# PROBLEM: No referential integrity for polymorphics
belongs_to :commentable, polymorphic: true
# SOLUTION: Validate type + consider alternatives
ALLOWED_TYPES = %w[Post Article].freeze
validates :commentable_type, inclusion: { in: ALLOWED_TYPES }
# Better: separate tables instead of polymorphic
belongs_to :post, optional: true
belongs_to :article, optional: true
validates :post_id, presence: true, unless: :article_id?
# Find orphaned comments (missing parent)
Comment.left_joins(:post).where(posts: { id: nil })
# Find missing foreign keys
ActiveRecord::Base.connection.tables.each do |table|
columns = ActiveRecord::Base.connection.columns(table)
columns.select { |c| c.name.end_with?('_id') }.each do |col|
# Check if FK exists
end
end
uniqueness validations have unique indexes?presence validations have NOT NULL constraints?_id columns have foreign keys?## Constraints Review: [PASS/WARN/FAIL]
### Missing Database Constraints
- [model]: [validation without DB enforcement]
### Foreign Key Issues
- [table]: [missing FK on _id column]
### Race Condition Risks
- [code location]: [check-then-insert pattern]
### Orphan Risks
- [association]: [could create orphans]
### Recommendations
1. [Prioritized fixes]
npx claudepluginhub majesticlabs-dev/majestic-marketplace --plugin majestic-railsGuides ActiveRecord in Rails: write migrations, define associations (belongs_to, has_many, through), optimize queries (includes, preload, eager_load), fix N+1 issues, add validations/callbacks, and handle database constraints.
Audits PostgreSQL/MySQL data for integrity issues (NULLs, orphans, formats, ranges, duplicates) and generates/enforces CHECK constraints, foreign keys, triggers.
Constraints, triggers, audit trails, referential integrity, and data validation.