cosmo_backend/app/models/db/celestial_body.py

50 lines
2.1 KiB
Python
Raw Permalink Normal View History

2025-12-02 06:29:38 +00:00
"""
CelestialBody ORM model
"""
2025-12-06 09:06:10 +00:00
from sqlalchemy import Column, String, Text, TIMESTAMP, Boolean, Integer, ForeignKey, CheckConstraint, Index
2025-12-02 06:29:38 +00:00
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.sql import func
from sqlalchemy.orm import relationship
from app.database import Base
class CelestialBody(Base):
"""Celestial body (star, planet, probe, etc.)"""
__tablename__ = "celestial_bodies"
id = Column(String(50), primary_key=True, comment="JPL Horizons ID or custom ID")
name = Column(String(200), nullable=False, comment="English name")
name_zh = Column(String(200), nullable=True, comment="Chinese name")
type = Column(String(50), nullable=False, comment="Body type")
2025-12-06 09:06:10 +00:00
system_id = Column(Integer, ForeignKey('star_systems.id', ondelete='CASCADE'), nullable=True, comment="所属恒星系ID")
2025-12-02 06:29:38 +00:00
description = Column(Text, nullable=True, comment="Description")
2025-12-05 16:36:39 +00:00
details = Column(Text, nullable=True, comment="Detailed description (Markdown)")
2025-12-02 06:29:38 +00:00
is_active = Column(Boolean, nullable=True, comment="Active status for probes (True=active, False=inactive)")
extra_data = Column(JSONB, nullable=True, comment="Extended metadata (JSON)")
created_at = Column(TIMESTAMP, server_default=func.now())
updated_at = Column(TIMESTAMP, server_default=func.now(), onupdate=func.now())
# Relationships
2025-12-06 09:06:10 +00:00
star_system = relationship("StarSystem", back_populates="celestial_bodies")
2025-12-02 06:29:38 +00:00
positions = relationship(
"Position", back_populates="body", cascade="all, delete-orphan"
)
resources = relationship(
"Resource", back_populates="body", cascade="all, delete-orphan"
)
# Constraints
__table_args__ = (
CheckConstraint(
"type IN ('star', 'planet', 'moon', 'probe', 'comet', 'asteroid', 'dwarf_planet', 'satellite')",
name="chk_type",
),
Index("idx_celestial_bodies_type", "type"),
Index("idx_celestial_bodies_name", "name"),
2025-12-06 09:06:10 +00:00
Index("idx_celestial_bodies_system", "system_id"),
2025-12-02 06:29:38 +00:00
)
def __repr__(self):
return f"<CelestialBody(id='{self.id}', name='{self.name}', type='{self.type}')>"