Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Define database models with clear naming, appropriate data types, constraints, relationships, and validation at multiple layers. Use this skill when creating or modifying database model files, ORM classes, schema definitions, or data model relationships. Apply when working with model files (e.g., models.py, models/, ActiveRecord classes, Prisma schema, Sequelize models), defining table structures, setting up foreign keys and relationships, configuring cascade behaviors, implementing model valida
.claude/skills/microck-backend-models-standards/SKILL.md| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-01 | ✗→✓ | ▲ Improved | 70% | 0% |
| case-02 | ✗→✓ | ▲ Improved | 106% | 0% |
| case-04 | ✗→✓ | ▲ Improved | 42% | 0% |
| case-06 | ✗→✓ | ▲ Improved | 263% | 0% |
| case-09 | ✗→✓ | ▲ Improved | 196% | 0% |
Core Rule: Models define data structure and integrity. Keep them focused on data representation, not business logic.
This Skill provides Claude Code with specific guidance on how to adhere to coding standards as they relate to how it should handle backend models.
Models: Singular, PascalCase (User, OrderItem, PaymentMethod)
Tables: Plural, snake_case (users, order_items, payment_methods)
Relationships: Descriptive and clear
user.orders (one-to-many)order.items (one-to-many)product.categories (many-to-many)Avoid generic names: data, info, record, entity
Timestamps on every model:
pythoncreated_at = Column(DateTime, nullable=False, default=datetime.utcnow) updated_at = Column(DateTime, nullable=False, default=datetime.utcnow, onupdate=datetime.utcnow)
Primary keys: Always explicit, prefer UUIDs for distributed systems or auto-incrementing integers for simplicity
Why: Auditing, debugging, data lineage tracking, soft deletes
Use constraints, not just application validation:
python# NOT NULL for required fields email = Column(String(255), nullable=False) # UNIQUE constraints email = Column(String(255), unique=True, nullable=False) # CHECK constraints for business rules age = Column(Integer, CheckConstraint('age >= 18')) # Foreign keys with explicit cascade behavior user_id = Column(Integer, ForeignKey('users.id', ondelete='CASCADE'))
Why: Database enforces rules even if application code bypassed. Defense in depth.
| Data | Type | Avoid | | ---------- | ------------------ | ----------- | | Email, URL | VARCHAR(255) | TEXT | | Short text | VARCHAR(n) | TEXT | | Long text | TEXT | VARCHAR | | Money | DECIMAL(10,2) | FLOAT | | Boolean | BOOLEAN | TINYINT | | Timestamps | TIMESTAMP/DATETIME | VARCHAR | | JSON data | JSON/JSONB | TEXT | | UUIDs | UUID | VARCHAR(36) |
Why: Correct types enable database optimizations, constraints, and prevent data corruption.
Always index:
Example:
pythonclass Order(Base): __tablename__ = 'orders' id = Column(Integer, primary_key=True) user_id = Column(Integer, ForeignKey('users.id'), index=True) status = Column(String(50), index=True) # Frequently filtered created_at = Column(DateTime, index=True) # Frequently sorted
Don't over-index: Each index slows writes. Index only queried columns.
Define both sides of relationships:
python# One-to-many class User(Base): orders = relationship('Order', back_populates='user', cascade='all, delete-orphan') class Order(Base): user_id = Column(Integer, ForeignKey('users.id')) user = relationship('User', back_populates='orders')
Cascade behaviors:
CASCADE: Delete related records (user deleted → orders deleted)SET NULL: Nullify foreign key (category deleted → product.category_id = NULL)RESTRICT: Prevent deletion if related records existNO ACTION: Database default, usually same as RESTRICTChoose based on business logic, not convenience.
Model-level validation (application):
python@validates('email') def validate_email(self, key, email): if not re.match(r'^[^@]+@[^@]+\.[^@]+$', email): raise ValueError('Invalid email format') return email
Database-level constraints (see Data Integrity section)
Why both: Model validation provides clear error messages. Database constraints prevent data corruption if application bypassed.
YES:
@property def full_name)NO:
Models represent data structure, not behavior.
Normalize when:
Denormalize when:
Default to normalized. Denormalize only with evidence of performance issues.
Soft deletes:
pythondeleted_at = Column(DateTime, nullable=True, index=True) # Query only active records query = session.query(User).filter(User.deleted_at.is_(None))
Polymorphic associations:
python# Avoid if possible - complex and hard to maintain # Prefer separate relationship fields or inheritance
Enums for fixed values:
pythonfrom enum import Enum class OrderStatus(str, Enum): PENDING = 'pending' PAID = 'paid' SHIPPED = 'shipped' DELIVERED = 'delivered' status = Column(Enum(OrderStatus), nullable=False, default=OrderStatus.PENDING)
Test constraints and validation:
pythondef test_user_email_required(): with pytest.raises(IntegrityError): user = User(name='Test') session.add(user) session.commit() def test_user_email_unique(): user1 = User(email='test@example.com') user2 = User(email='test@example.com') session.add(user1) session.commit() with pytest.raises(IntegrityError): session.add(user2) session.commit()
Test relationships:
pythondef test_user_orders_cascade_delete(): user = User(email='test@example.com') order = Order(user=user) session.add(user) session.commit() session.delete(user) session.commit() assert session.query(Order).count() == 0
created_at and updated_at timestamps| Case | Status | Duration (ms) | Turns | Tokens | Tool calls | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Without | With | Δ | Without | With | Δ | Without | With | Δ | Without | With | Δ | ||
case-01 | fail→pass | 14,706 | 15,415 | +5% | 1 | 1 | 0% | 2,936 | 5,005 | +70% | 0 | 0 | — |
case-02 | fail→pass | 9,708 | 12,617 | +30% | 1 | 1 | 0% | 2,072 | 4,267 | +106% | 0 | 0 | — |
case-03 | pass→pass | 9,000 | 7,338 | -18% | 1 | 1 | 0% | 1,723 | 3,262 | +89% | 0 | 0 | — |
case-04 | fail→pass | 12,253 | 7,242 | -41% | 1 | 1 | 0% | 2,241 | 3,174 | +42% | 0 | 0 | — |
case-05 | pass→pass | 13,493 | 9,915 | -27% | 1 | 1 | 0% | 2,407 | 3,683 | +53% | 0 | 0 | — |
case-06 | fail→pass | 5,701 | 11,445 | +101% | 1 | 1 | 0% | 1,102 | 4,005 | +263% | 0 | 0 | — |
case-07 | pass→pass | 12,090 | 8,582 | -29% | 1 | 1 | 0% | 2,228 | 3,366 | +51% | 0 | 0 | — |
case-08 | pass→pass | 6,687 | 6,632 | -1% | 1 | 1 | 0% | 1,330 | 3,161 | +138% | 0 | 0 | — |
case-09 | fail→pass | 5,455 | 6,736 | +23% | 1 | 1 | 0% | 1,000 | 2,962 | +196% | 0 | 0 | — |
case-10 | fail→pass | 6,981 | 6,601 | -5% | 1 | 1 | 0% | 1,327 | 3,044 | +129% | 0 | 0 | — |
case-11 | pass→pass | 9,265 | 8,888 | -4% | 1 | 1 | 0% | 1,717 | 3,519 | +105% | 0 | 0 | — |
case-12 | pass→pass | 9,436 | 9,149 | -3% | 1 | 1 | 0% | 1,756 | 3,624 | +106% | 0 | 0 | — |
case-13 | fail→pass | 12,872 | 8,728 | -32% | 1 | 1 | 0% | 2,361 | 3,466 | +47% | 0 | 0 | — |
case-14 | fail→pass | 6,976 | 9,182 | +32% | 1 | 1 | 0% | 1,325 | 3,699 | +179% | 0 | 0 | — |
case-15 | pass→fail | 9,457 | 9,485 | +0% | 1 | 1 | 0% | 1,734 | 3,595 | +107% | 0 | 0 | — |
case-16 | fail→pass | 14,103 | 11,965 | -15% | 1 | 1 | 0% | 2,787 | 4,182 | +50% | 0 | 0 | — |
case-17 | pass→pass | 6,598 | 4,972 | -25% | 1 | 1 | 0% | 1,175 | 2,672 | +127% | 0 | 0 | — |
case-18 | pass→pass | 8,074 | 4,005 | -50% | 1 | 1 | 0% | 1,602 | 2,586 | +61% | 0 | 0 | — |
case-19 | pass→pass | 6,456 | 4,950 | -23% | 1 | 1 | 0% | 1,131 | 2,754 | +144% | 0 | 0 | — |
case-20 | pass→pass | 17,150 | 12,425 | -28% | 1 | 1 | 0% | 3,448 | 4,413 | +28% | 0 | 0 | — |
case-21 | pass→pass | 5,810 | 7,993 | +38% | 1 | 1 | 0% | 1,162 | 3,573 | +207% | 0 | 0 | — |
case-22 | pass→pass | 8,272 | 6,878 | -17% | 1 | 1 | 0% | 1,598 | 3,142 | +97% | 0 | 0 | — |
DecimalAI ran this skill against gemini-3.6-flash twice over the same eval suite — once with the skill loaded and once without — and compared the two runs case by case. 22 cases were attempted. The headline lift of +36 percentage points is the difference between those two pass rates over the 22 comparable cases. 1 case got worse with the skill loaded, and it is included in that figure.
Without the skill loaded, the model failed this case. With it loaded, the same prompt on the same model passed. This is one improved case from the latest verified run; every case, including any that regressed, is in the table above.
Other measured skills in the registry, with their headline benchmark lift.