attachment_binding.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. """主案与共享附件挂载关系模型。"""
  2. from __future__ import annotations
  3. from sqlalchemy import BigInteger, Computed, ForeignKey, Index, Integer, UniqueConstraint, text
  4. from sqlalchemy.orm import Mapped, mapped_column
  5. from dms.database.base import BusinessTableMixin
  6. from dms.extensions import db
  7. class AttachmentBinding(BusinessTableMixin, db.Model):
  8. __tablename__ = "doc_attachment_binding"
  9. __table_args__ = (
  10. UniqueConstraint(
  11. "main_document_id",
  12. "attachment_document_id",
  13. "active_marker",
  14. name="uq_doc_attachment_binding_active",
  15. ),
  16. Index(
  17. "ix_doc_attachment_binding_main_active_sort",
  18. "main_document_id",
  19. "is_deleted",
  20. "sort_no",
  21. ),
  22. Index(
  23. "ix_doc_attachment_binding_attachment_active",
  24. "attachment_document_id",
  25. "is_deleted",
  26. ),
  27. )
  28. main_document_id: Mapped[int] = mapped_column(
  29. BigInteger,
  30. ForeignKey("doc_document.id", ondelete="RESTRICT"),
  31. nullable=False,
  32. )
  33. attachment_document_id: Mapped[int] = mapped_column(
  34. BigInteger,
  35. ForeignKey("doc_document.id", ondelete="RESTRICT"),
  36. nullable=False,
  37. )
  38. sort_no: Mapped[int] = mapped_column(
  39. Integer,
  40. nullable=False,
  41. server_default=text("0"),
  42. )
  43. active_marker: Mapped[int | None] = mapped_column(
  44. Integer,
  45. Computed("IF(is_deleted = 0, 1, NULL)", persisted=False),
  46. nullable=True,
  47. )