Skip to main content

Overview

The Color model represents colors available in the furniture catalog. It includes validation, audit trail fields, and soft delete functionality. Table Name: colors

Fields

Primary Key

id_color
Integer
required
Unique identifier for the color. Auto-incremented primary key.

Core Fields

name
String(50)
required
Name of the color. Must be unique across all colors.Constraints:
  • Maximum length: 50 characters
  • Unique: true
  • Nullable: false
active
Boolean
default:"true"
required
Indicates whether the color is active in the system.Default: true

Audit Trail Fields

created_at
TIMESTAMP
required
Timestamp when the color was created.Default: Current timestamp (server-side)
updated_at
TIMESTAMP
required
Timestamp when the color was last updated. Automatically updated on record modification.Default: Current timestamp (server-side)On Update: Automatically set to current timestamp
deleted_at
TIMESTAMP
Timestamp when the color was soft-deleted. NULL if the record is not deleted.
created_by
String(100)
Username or identifier of the user who created the color.
updated_by
String(100)
Username or identifier of the user who last updated the color.
deleted_by
String(100)
Username or identifier of the user who soft-deleted the color.

Methods

to_dict()

Serializes the Color model instance to a dictionary format suitable for JSON responses. Returns: dict
id_color
integer
The color’s unique identifier
name
string
The color name
active
boolean
Active status of the color
created_at
string
ISO 8601 formatted creation timestamp
updated_at
string
ISO 8601 formatted last update timestamp
Example Response:
{
  "id_color": 1,
  "name": "Mahogany",
  "active": true,
  "created_at": "2024-01-15T10:30:00",
  "updated_at": "2024-01-15T10:30:00"
}

Model Source Code

from sqlalchemy.sql import func

from ..extensions import db


class Color(db.Model):
    """
    Modelo de Color para catálogo de referencias de colores.

    Attributes:
          id_color: Identificador único del color.
          name: Nombre del color.
          active: Indica si el color está activo o no.

          created_at: Fecha de creación del color.
          updated_at: Fecha de última actualización del color.
          deleted_at: Fecha de eliminación lógica del color.

          created_by: Usuario que creó el color.
          updated_by: Usuario que actualizó el color.
          deleted_by: Usuario que eliminó el color.
    """

    __tablename__ = 'colors'

    id_color = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50), nullable=False, unique=True)
    active = db.Column(db.Boolean, nullable=False, default=True)

    created_at = db.Column(
        db.TIMESTAMP,
        nullable=False,
        server_default=func.current_timestamp()
    )
    updated_at = db.Column(
        db.TIMESTAMP,
        nullable=False,
        server_default=func.current_timestamp(),
        server_onupdate=func.current_timestamp()
    )
    deleted_at = db.Column(db.TIMESTAMP, nullable=True)

    created_by = db.Column(db.String(100), nullable=True)
    updated_by = db.Column(db.String(100), nullable=True)
    deleted_by = db.Column(db.String(100), nullable=True)

    def to_dict(self) -> dict:
        """
        Serializa el modelo a diccionario.

        Returns:
            dict: Representación del color en formato diccionario
        """
        return {
            "id_color": self.id_color,
            "name": self.name,
            "active": self.active,
            "created_at": self.created_at.isoformat() if self.created_at else None,
            "updated_at": self.updated_at.isoformat() if self.updated_at else None,
        }

Notes

  • The model implements soft delete functionality via the deleted_at field
  • Timestamps are managed automatically by the database
  • The to_dict() method excludes soft-delete audit fields from the response
  • All audit trail *_by fields accept up to 100 characters for user identification

Build docs developers (and LLMs) love