43 lines
1.1 KiB
Python
43 lines
1.1 KiB
Python
from datetime import UTC, datetime
|
|
|
|
from app import db
|
|
|
|
|
|
class CardLabel(db.Model):
|
|
"""Many-to-many relationship between cards and labels"""
|
|
|
|
__tablename__ = "card_labels"
|
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
|
card_id = db.Column(
|
|
db.Integer,
|
|
db.ForeignKey("cards.id", ondelete="CASCADE"),
|
|
nullable=False,
|
|
index=True,
|
|
)
|
|
label_id = db.Column(
|
|
db.Integer,
|
|
db.ForeignKey("labels.id", ondelete="CASCADE"),
|
|
nullable=False,
|
|
index=True,
|
|
)
|
|
|
|
# Timestamps
|
|
created_at = db.Column(db.DateTime, default=lambda: datetime.now(UTC))
|
|
|
|
# Unique constraint to prevent duplicate associations
|
|
__table_args__ = (
|
|
db.UniqueConstraint("card_id", "label_id", name="_card_label_uc"),
|
|
)
|
|
|
|
def to_dict(self):
|
|
"""Convert card label to dictionary"""
|
|
return {
|
|
"id": self.id,
|
|
"card_id": self.card_id,
|
|
"label_id": self.label_id,
|
|
"created_at": self.created_at.isoformat() if self.created_at else None,
|
|
}
|
|
|
|
def __repr__(self):
|
|
return f"<CardLabel card_id={self.card_id} label_id={self.label_id}>"
|