from datetime import datetime from app import db class GrowLog(db.Model): __tablename__ = "grow_logs" __table_args__ = {"extend_existing": True} id = db.Column(db.Integer, primary_key=True) plant_id = db.Column(db.Integer, db.ForeignKey("plant.id"), nullable=False) title = db.Column(db.String(255), nullable=True) notes = db.Column(db.Text, nullable=True) is_public = db.Column(db.Boolean, default=False, nullable=False) created_at = db.Column(db.DateTime, default=datetime.utcnow, nullable=False) updated_at = db.Column( db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False ) # ↔ images uploaded directly to this GrowLog media_items = db.relationship( "plugins.media.models.Media", back_populates="growlog", foreign_keys="plugins.media.models.Media.growlog_id", lazy="dynamic", cascade="all, delete-orphan", ) # ↔ child updates updates = db.relationship( "plugins.growlog.models.PlantUpdate", back_populates="growlog", foreign_keys="plugins.growlog.models.PlantUpdate.growlog_id", lazy="dynamic", cascade="all, delete-orphan", ) class PlantUpdate(db.Model): __tablename__ = "plant_updates" __table_args__ = {"extend_existing": True} id = db.Column(db.Integer, primary_key=True) growlog_id = db.Column(db.Integer, db.ForeignKey("grow_logs.id"), nullable=False) description = db.Column(db.Text, nullable=True) created_at = db.Column(db.DateTime, default=datetime.utcnow, nullable=False) # ↔ parent GrowLog.updates growlog = db.relationship( "plugins.growlog.models.GrowLog", back_populates="updates", foreign_keys=[growlog_id], lazy="joined", ) # ↔ images attached via UpdateImage join table media_items = db.relationship( "plugins.growlog.models.UpdateImage", back_populates="update", foreign_keys="plugins.growlog.models.UpdateImage.update_id", lazy="dynamic", cascade="all, delete-orphan", ) class UpdateImage(db.Model): __tablename__ = "update_images" __table_args__ = {"extend_existing": True} id = db.Column(db.Integer, primary_key=True) update_id = db.Column(db.Integer, db.ForeignKey("plant_updates.id"), nullable=False) media_id = db.Column(db.Integer, db.ForeignKey("media.id"), nullable=False) created_at = db.Column(db.DateTime, default=datetime.utcnow, nullable=False) # ↔ PlantUpdate.media_items update = db.relationship( "plugins.growlog.models.PlantUpdate", back_populates="media_items", foreign_keys=[update_id], lazy="joined", ) # ↔ the actual Media record media = db.relationship( "plugins.media.models.Media", backref=db.backref("update_images", lazy="dynamic"), foreign_keys=[media_id], lazy="joined", )