This code defines a class `Shipment` using SQLAlchemy, an object-relational...

August 27, 2025 at 08:15 AM

class Shipment(Base): __tablename__ = "shipments" id = Column(Integer, primary_key=True, index=True) shipment_number = Column(String(50), unique=True, nullable=False) contract_id = Column(Integer, ForeignKey("contract.id")) agency_id = Column(Integer, ForeignKey("agency.id")) vessel_name = Column(String(100)) voyage_number = Column(String(50)) cargo_type = Column(String(100)) cargo_description = Column(Text) quantity = Column(Numeric(12, 2)) unit_of_measure = Column(String(20)) loading_port = Column(String(100)) discharge_port = Column(String(100)) loading_date = Column(Date) discharge_date = Column(Date) estimated_arrival = Column(Date) actual_arrival = Column(Date) status = Column(String(30), default="planned") operation_remarks = Column(Text) marketing_remarks = Column(Text) special_instructions = Column(Text) created_by = Column(Integer, ForeignKey("users.id")) assigned_to = Column(Integer, ForeignKey("users.id")) created_at = Column(DateTime(timezone=True), server_default=func.now()) updated_at = Column(DateTime(timezone=True), server_default=func.now(), onupdate=func.now()) # Relationships contract = relationship("Contract", back_populates="shipments") agency = relationship("Agency", back_populates="shipments") creator = relationship("User", foreign_keys=[created_by], back_populates="created_shipments") assignee = relationship("User", foreign_keys=[assigned_to], back_populates="assigned_shipments") transactions = relationship("FinancialTransaction", back_populates="shipment")

This code defines a class Shipment using SQLAlchemy, an object-relational mapping (ORM) library in Python, for interacting with a relational database. Here's what the code does in detail:

  1. Maps to a Database Table:

    • The class name Shipment represents a table named "shipments" in the database, defined by __tablename__ = "shipments".
  2. Defines Columns: Each attribute in the class represents a column in the database table, with specific constraints and types.

    • id: Primary key, an integer column that uniquely identifies each shipment. It is also indexed for faster retrieval.
    • shipment_number: A unique identifier for the shipment (up to 50 characters), cannot be null.
    • contract_id and agency_id: Foreign keys referencing the contract and agency tables respectively ("contract.id" and "agency.id").
    • Other columns like vessel_name, voyage_number, cargo_type, and others store shipment details such as vessel information, cargo, ports, dates, and status.
    • Fields like created_at and updated_at automatically store the timestamp for creation and updates using func.now().
    • created_by and assigned_to: Foreign keys pointing to a users table to store which user created or was assigned the shipment.
  3. Relationships between Tables: Relationships establish links between the Shipment table and other tables:

    • contract: A relationship to the Contract table (uses the shipments attribute in the Contract class for back-population).
    • agency: A relationship to the Agency table (uses the shipments attribute in the Agency class for back-population).
    • creator and assignee: Relationships to the User table, linking shipments to the user who created or was assigned to the shipment.
    • transactions: A one-to-many or many-to-many relationship to the FinancialTransaction table for related transactions.
  4. Constraints and Defaults:

    • Sets constraints like nullable=False to ensure required fields (shipment_number) cannot be left empty.
    • Includes defaults for columns like status (planned) and auto-updating timestamps with onupdate.
  5. Data Types:

    • Uses various SQLAlchemy column types such as String for text, Text for larger text, Integer for numbers, Numeric for decimal numbers with precision, Date for dates, and DateTime for time-stamped fields.

In summary, this class represents a shipment record in the database, complete with its fields, constraints, relationships, and other metadata for ORM mapping. It allows you to query associated contracts, users, agencies, and financial transactions in a relational and programmatic way.

Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node