Skip to main content

Overview

The UnitOfMeasure model represents units of measurement used throughout the furniture store system (e.g., cm, m, kg, pieces). Each unit includes both a full name and an abbreviation. Table Name: unit_of_measures

Fields

Primary Key

id_unit_of_measure
Integer
required
Unique identifier for the unit of measure. Auto-incremented primary key.

Core Fields

name
String(50)
required
Full name of the unit (e.g., “Centimeter”, “Kilogram”). Must be unique.Constraints:
  • Maximum length: 50 characters
  • Unique: true
  • Nullable: false
abbreviation
String(10)
required
Abbreviated form of the unit (e.g., “cm”, “kg”, “pcs”). Must be unique.Constraints:
  • Maximum length: 10 characters
  • Unique: true
  • Nullable: false
active
Boolean
default:"true"
required
Indicates whether the unit of measure is active in the system.Default: true

Audit Trail Fields

created_at
TIMESTAMP
required
Timestamp when the unit of measure was created.Default: Current timestamp (server-side)
updated_at
TIMESTAMP
required
Timestamp when the unit of measure 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 unit of measure was soft-deleted. NULL if the record is not deleted.
created_by
String(100)
Username or identifier of the user who created the unit of measure.
updated_by
String(100)
Username or identifier of the user who last updated the unit of measure.
deleted_by
String(100)
Username or identifier of the user who soft-deleted the unit of measure.

Methods

to_dict()

Serializes the UnitOfMeasure model instance to a dictionary format suitable for JSON responses. Returns: dict
id_unit_of_measure
integer
The unit’s unique identifier
name
string
The full name of the unit
abbreviation
string
The abbreviated form of the unit
active
boolean
Active status of the unit
created_at
string
ISO 8601 formatted creation timestamp
updated_at
string
ISO 8601 formatted last update timestamp
Example Response:
{
  "id_unit_of_measure": 1,
  "name": "Centimeter",
  "abbreviation": "cm",
  "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 UnitOfMeasure(db.Model):
    """
    Modelo de Unidades de medida para catálogo de referencias de unidades.

    Attributes:
          id_unit_of_measure: Identificador único de la unidad.
          name: Nombre de la unidad.
          active: Indica si la unidad está activo o no.
          abbreviation: Abreviatura de la unidad.

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

          created_by: Usuario que creó la unidad.
          updated_by: Usuario que actualizó la unidad.
          deleted_by: Usuario que eliminó la unidad.
    """

    __tablename__ = 'unit_of_measures'

    id_unit_of_measure = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50), nullable=False, unique=True)
    abbreviation = db.Column(db.String(10), 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:
        """
        Convierte el objeto UnitOfMeasure a un diccionario.

        Returns:
            dict: Diccionario con los atributos del objeto UnitOfMeasure
        """
        return {
            "id_unit_of_measure": self.id_unit_of_measure,
            "name": self.name,
            "abbreviation": self.abbreviation,
            "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

  • Both name and abbreviation fields must be unique
  • 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
  • Commonly used for dimensions (cm, m, in) and quantities (pcs, kg, lb)

Build docs developers (and LLMs) love